| Version 2 (modified by , 11 years ago) ( diff ) |
|---|
Coding Standards
- General
- All code should compile without errors or warnings;
- Eclipse should indicate no warnings.
- Code Structure
- source code goes in src (under trunk)
- test code goes in test (under trunk)
- never use wildcards (*) in import statements
- a class should do one thing and do that one thing well
- keep them small
- break up into smaller classes if class becomes unwieldy
- break up a method into smaller pieces if it gets too long
- place one blank line between elements in a file (an element is a field, method, or class)
- one blank line before and after each block of local variables declarations
- Visibility
- 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)
- do not reveal more than absolutely necessary
- example: typically constructors can be package-private
- a public factory method is called by outside world
- best to start at most restrictive (private) and only increase visibility if there is no choice
- generally, fields should be private
- write access methods for those that need to be revealed
- methods declared in (Java) interfaces must be public so don't declare them to be (i.e., leave out the public)
- Names
- generally, package names are all lower case letters
- exception: IF
- class and interface names start with uppercase letters and are written in
CamelCase - method and variable names generally start with lower case letters and are written using
camelCase- exception: "constants" generally all caps:
public final static int MEM_BOUND = 2060;
- exception: names of enum elements:
public enum Result {YES, NO, MAYBE};
- exception: "constants" generally all caps:
- avoid abbreviations in identifiers
- For example "expression", not "expr"
- use meaningful names for identifiers that explain their purpose
- Documentation
- See http://docs.oracle.com/javase/7/docs/api/ for good examples of javadocs
- See http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
- every element (method, field, class, interface, ...) has a javadoc
- javadocs should explain
- ask yourself would someone who has never seen this code before be able to read this and understand what it does?
- tags
- which tags occur in javadocs is not nearly as important as the clarity and completeness of the explanation
- do not include a tag with no information associated to it
- do include an
@authortag for each file - do include an
@exceptiontag for each exception (including runtime exceptions) that could be thrown
- javadocs for interface methods should not reveal implementation details
- 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
- method javadocs should list the pre-conditions and post-conditions for the method
- 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. - 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)
- 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.,
- packages should have a javadoc in a file
package-info.java
Note:
See TracWiki
for help on using the wiki.
