| | 1 | |
| | 2 | == Coding Standards == |
| | 3 | |
| | 4 | - General |
| | 5 | |
| | 6 | 1. All code should compile without errors or warnings; |
| | 7 | 2. Eclipse should indicate no warnings. |
| | 8 | |
| | 9 | - Code Structure |
| | 10 | |
| | 11 | 1. source code goes in src (under trunk) |
| | 12 | 2. test code goes in test (under trunk) |
| | 13 | 3. never use wildcards (*) in import statements |
| | 14 | 4. a class should do one thing and do that one thing well |
| | 15 | - keep them small |
| | 16 | - break up into smaller classes if class becomes unwieldy |
| | 17 | 5. break up a method into smaller pieces if it gets too long |
| | 18 | 6. place one blank line between elements in a file (an element is a field, method, or class) |
| | 19 | 7. all local variables declared in a block (i.e., a compound statement in curly braces) should appear at the top of the block, followed by a blank line, followed by the statements in that block; this greatly enhances readability |
| | 20 | |
| | 21 | - Visibility |
| | 22 | 1. think carefully about the visibility level for each element, i.e., whether to make the element public (visible to all), protected (visible to package and sub-classes), package-private (visible to package, the default in Java), or private (visible to same class only) |
| | 23 | 2. do not reveal more than absolutely necessary |
| | 24 | 3. example: typically constructors can be package-private |
| | 25 | - a public factory method is called by outside world |
| | 26 | 4. best to start at most restrictive (private) and only increase visibility if there is no choice |
| | 27 | 5. generally, fields should be private |
| | 28 | - write access methods for those that need to be revealed |
| | 29 | 6. methods declared in (Java) interfaces must be public so don't declare them to be (i.e., leave out the public) |
| | 30 | |
| | 31 | - Names |
| | 32 | 1. generally, package names are all lower case letters |
| | 33 | - exception: IF |
| | 34 | 2. class and interface names start with uppercase letters and are written in {{{CamelCase}}} |
| | 35 | 3. method and variable names generally start with lower case letters and are written using {{{camelCase}}} |
| | 36 | - exception: "constants" generally all caps: |
| | 37 | - {{{public final static int MEM_BOUND = 2060;}}} |
| | 38 | - exception: names of enum elements: |
| | 39 | - {{{public enum Result {YES, NO, MAYBE};}}} |
| | 40 | 4. avoid abbreviations in identifiers |
| | 41 | - For example "expression", not "expr" |
| | 42 | - use meaningful names for identifiers that explain their purpose |
| | 43 | |
| | 44 | - Documentation |
| | 45 | |
| | 46 | 1. See [http://docs.oracle.com/javase/7/docs/api/] for good examples of javadocs |
| | 47 | 2. See [http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html] |
| | 48 | 3. every element (method, field, class, interface, ...) has a javadoc |
| | 49 | 4. javadocs should ''explain'' |
| | 50 | - ask yourself ''would someone who has never seen this code before be able to read this and understand what it does?'' |
| | 51 | 5. tags |
| | 52 | - which tags occur in javadocs is not nearly as important as the clarity and completeness of the explanation |
| | 53 | - do not include a tag with no information associated to it |
| | 54 | - do include an {{{@author}}} tag for each file |
| | 55 | - do include an {{{@exception}}} tag for each exception (including runtime exceptions) that could be thrown |
| | 56 | 6. javadocs for interface methods should not reveal implementation details |
| | 57 | 7. javadocs for methods in a class that implement a method declared in an interface can simply refer to the interface javadoc and add any additional information specific to that implementation; do not copy and paste the interface javadoc |
| | 58 | 8. method javadocs should list the pre-conditions and post-conditions for the method |
| | 59 | - a pre-condition is a condition the program state should satisfy when the method is called. This is typically a condition on the parameters to the method, e.g., {{{N>0}}}. |
| | 60 | - a post-condition is a guarantee: it states a condition the program state must satisfy when the method returns (assuming the method was called in a way that satisfied the pre-condition) |
| | 61 | 9. packages should have a javadoc in a file {{{package-info.java}}} |