|
a |
|
b/docs/contributing/Code_Conventions.md |
|
|
1 |
## DNAnalyzer Code Conventions |
|
|
2 |
|
|
|
3 |
You should keep these practices in mind all the time while contributing to DNAnalyzer. |
|
|
4 |
|
|
|
5 |
### Source File Name |
|
|
6 |
|
|
|
7 |
- The source file name consists of the case-sensitive name of the top-level class it contains, plus the .java extension. |
|
|
8 |
|
|
|
9 |
### Source File Structure |
|
|
10 |
|
|
|
11 |
- A source file consists of, in order: |
|
|
12 |
- License or copyright information, if present |
|
|
13 |
- Package statement |
|
|
14 |
- Import statements |
|
|
15 |
- Exactly one top-level class |
|
|
16 |
|
|
|
17 |
- Exactly one blank line separates each section that is present. |
|
|
18 |
- All "special variables" (i.e. constants to be initialized early) are declared at the top of the file, below any |
|
|
19 |
imports. |
|
|
20 |
|
|
|
21 |
### Imports |
|
|
22 |
|
|
|
23 |
- Wildcard imports, static or otherwise, are not used. |
|
|
24 |
- Import statements are not line-wrapped. |
|
|
25 |
|
|
|
26 |
- Imports are ordered as follows: |
|
|
27 |
- All static imports in a single block. |
|
|
28 |
- All non-static imports in a single block. |
|
|
29 |
- If there are both static and non-static imports, a single blank line separates the two blocks. There are no other |
|
|
30 |
blank lines between import statements. |
|
|
31 |
- Static import is not used for static nested classes. They are imported with normal imports. |
|
|
32 |
|
|
|
33 |
### Class Declaration |
|
|
34 |
|
|
|
35 |
- Each top-level class resides in a source file of its own. |
|
|
36 |
- Methods of a class that share the same name appear in a single contiguous group with no other members in between. The |
|
|
37 |
same applies to multiple constructors (which always have the same name). This rule applies even when modifiers such as |
|
|
38 |
static or private differ between the methods. |
|
|
39 |
|
|
|
40 |
### Formatting |
|
|
41 |
|
|
|
42 |
- Braces |
|
|
43 |
- Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single |
|
|
44 |
statement. |
|
|
45 |
- No line break before the opening brace, except as detailed below. |
|
|
46 |
- Line break after the opening brace. |
|
|
47 |
- Line break before the closing brace. |
|
|
48 |
- Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, |
|
|
49 |
constructor, or named class. For example, there is no line break after the brace if it is followed by else or a |
|
|
50 |
comma. |
|
|
51 |
- An empty block or block-like construct may be closed immediately after it is opened, with no characters or line |
|
|
52 |
break in between ({}). |
|
|
53 |
|
|
|
54 |
- Block Indentation |
|
|
55 |
- Each time a new block or block-like construct is opened, the indent increases by two spaces. When the block ends, |
|
|
56 |
the indent returns to the previous indent level. The indent level applies to both code and comments throughout the |
|
|
57 |
block. |
|
|
58 |
|
|
|
59 |
- Whitespace |
|
|
60 |
- Indents shall be 4 spaces, no tabs. |
|
|
61 |
- [set this as your setting on vscode](https://stackoverflow.com/a/38556923) |
|
|
62 |
|
|
|
63 |
- Comments |
|
|
64 |
- Comments NEVER go on a line that already has code in it |
|
|
65 |
- Make comments succinct and ONLY about important code functionality; ex. `// connects to database` is |
|
|
66 |
OK, `// prints to console` is not |
|
|
67 |
- If you don’t need the code, delete it, don't comment it out. |
|
|
68 |
|
|
|
69 |
### Naming |
|
|
70 |
|
|
|
71 |
- Package names |
|
|
72 |
- Package names use only lowercase letters and digits (no underscores). Consecutive words are simply concatenated |
|
|
73 |
together. For example, com.example.deepspace, not com.example.deepSpace or com.example.deep_space. |
|
|
74 |
|
|
|
75 |
- Class names |
|
|
76 |
- Class names are written in UpperCamelCase. |
|
|
77 |
- Class names are typically nouns or noun phrases. For example, Character or ImmutableList. Interface names may also |
|
|
78 |
be nouns or noun phrases (for example, List), but may sometimes be adjectives or adjective phrases instead (for |
|
|
79 |
example, Readable). |
|
|
80 |
|
|
|
81 |
- Method names |
|
|
82 |
- Method names are written in lowerCamelCase. |
|
|
83 |
- Method names are typically verbs or verb phrases. For example, sendMessage or stop. |
|
|
84 |
|
|
|
85 |
- Constant names |
|
|
86 |
- Constant names use UPPER_SNAKE_CASE: all uppercase letters, with each word separated from the next by a single |
|
|
87 |
underscore |
|
|
88 |
|
|
|
89 |
- Non-constant field names |
|
|
90 |
- Non-constant field names (static or otherwise) are written in lowerCamelCase.- These names are typically nouns or |
|
|
91 |
noun phrases. For example, computedValues or index. |
|
|
92 |
|
|
|
93 |
- Local variable names |
|
|
94 |
- Local variable names are written in lowerCamelCase. |
|
|
95 |
- Even when final and immutable, local variables are not considered to be constants, and should not be styled as |
|
|
96 |
constants. |
|
|
97 |
|
|
|
98 |
- Make names representative, no shorthands unless it’s obvious; i.e. "authentication"="auth" is ok, but "question"=" |
|
|
99 |
antsy" is not; gray areas such as "Question"="Ques" should be renamed (i.e. use "Question", not "Ques") |
|
|
100 |
|
|
|
101 |
### Best Practices |
|
|
102 |
|
|
|
103 |
- @Override is always used |
|
|
104 |
- A method is marked with the @Override annotation whenever it is legal. This includes a class method overriding a |
|
|
105 |
superclass method, a class method implementing an interface method, and an interface method re-specifying a |
|
|
106 |
superinterface method. |
|
|
107 |
- Caught exceptions are not ignored |
|
|
108 |
- it is very rarely correct to do nothing in response to a caught exception. (Typical responses are to log it, or if |
|
|
109 |
it is considered "impossible", rethrow it as an AssertionError.) |
|
|
110 |
- Reuse code when possible (i.e. if a thing is done in multiple places, see if a function is possible) |
|
|
111 |
- Make algorithms elegant if you see a better way to do the same task |
|
|
112 |
- Split functions that are big; each function should only do 1 task |
|
|
113 |
|
|
|
114 |
One last thing: if you’re refactoring a lot of code, remember to let the rest of the team know so that we can |
|
|
115 |
accommodate and avoid conflicts! |