Changes between Initial Version and Version 1 of Coding Standards


Ignore:
Timestamp:
01/15/14 17:26:12 (12 years ago)
Author:
zmanchun
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Coding Standards

    v1 v1  
     1
     2== Coding Standards ==
     3
     4- General
     5
     61. All code should compile without errors or warnings;
     72. Eclipse should indicate no warnings.
     8
     9- Code Structure
     10
     111. source code goes in src (under trunk)
     122. test code goes in test (under trunk)
     133. never use wildcards (*) in import statements
     144. 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
     175. break up a method into smaller pieces if it gets too long
     186. place one blank line between elements in a file (an element is a field, method, or class)
     197. 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
     221. 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)
     232. do not reveal more than absolutely necessary
     243. example: typically constructors can be package-private
     25    - a public factory method is called by outside world
     264. best to start at most restrictive (private) and only increase visibility if there is no choice
     275. generally, fields should be private
     28    - write access methods for those that need to be revealed
     296. methods declared in (Java) interfaces must be public so don't declare them to be (i.e., leave out the public)
     30
     31- Names
     321. generally, package names are all lower case letters
     33    - exception: IF
     342. class and interface names start with uppercase letters and are written in {{{CamelCase}}}
     353. 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};}}}
     404. avoid abbreviations in identifiers
     41    - For example "expression", not "expr"
     42    - use meaningful names for identifiers that explain their purpose
     43
     44- Documentation
     45
     461. See [http://docs.oracle.com/javase/7/docs/api/] for good examples of javadocs
     472. See [http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html]
     483. every element (method, field, class, interface, ...) has a javadoc
     494. 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?''
     515. 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
     566. javadocs for interface methods should not reveal implementation details
     577. 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
     588. 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)
     619. packages should have a javadoc in a file {{{package-info.java}}}