Use descriptive and appropriate names for all identifiers
(variables, method names, class names, constants, etc.).
Comment confusing sections of code.
Be neat.
Identifier Naming and Capitalization
Guidelines
Use descriptive names for all variables, function names, constants,
and other identifiers. Use single letter identifiers only for the
counter in loops.
Variable names start with lower case.
Multi-word identifiers are internally capitalized.
Do not use hyphens or underscores to separate multi-word
identifiers (exception only for those who use speech recognition
software).
Every class should be preceded with a descriptive comment using the
“JavaDoc” notational convention. The comment should name the class,
describe its purpose, and name the author.
Example
/**
* Haiku, prints a haiku.
* @author Suzuki, Susan
*/publicclassHaiku{// ...}
Comments: Methods
Guidelines
Every method should be preceded with a descriptive comment using the
“JavaDoc” notational convention. The comment should name the method,
describe its purpose, comment all arguments, the return value, and
any exceptions using JavaDoc keywords. (Omit @return and @exception,
if the return value is void or there are no exceptions thrown.)
Example
/**
* Attempts to print a word. Indicates whether printing was possible.
* @param word to print, must not contain spaces
* @return true if printer is available, false otherwise
* @exception SpacesFoundException if there are any spaces in the word
*/publicbooleanprintWord(Stringword)throwsSpacesFoundException{// ...}
Comments: Public variables
Guidelines
Every public variable should be preceded with a descriptive comment
using the “JavaDoc” notational convention. The comment should
describe the purpose for the public variable.
Example
/** Toggles between Frame and NoFrame mode. */publicbooleanframeMode=true;
Comments: In-line
Guidelines
In-line comments should be used to explain complicated sections of
code, such as loops. Use the // comment delimiter for in-line
comments. Do not comment generally known features of the java
language.
Example
// Do a 3D transmogrification on the matrix.for(inti=0;i<matrix.length;i++){for(intj=0;j<matrix.length;j++){for(intk=0;k<matrix.length;k++){values.transmogrify(matrix[i],matrix[j],matrix[k]);}}}
Counter Example
i++;// increments i// the variable "i" loops from 0 to the length of matrixfor(inti=0;i<matrix.length;i++){// ...}
Spacing: Between lines
Guidelines
Use two blank lines to separate each method within a class
definition. Use one blank line to separate logical sections of code
within a method.
Close braces (i.e. “}”) do start a new line, and are indented with the code they close.
Comments line up with the block they comment.
Example
for(inti=0;i<args.length;i=i+1){vals.insertElementAt(newFloat(args[i]),i);// Transmogrify is incremental and more efficient inside the loop.vals.transmogrify();}
Counter Example
for(inti=0;i<args.length;i=i+1){vals.insertElementAt(newFloat(args[i]),i);// Transmogrify is incremental and more efficient inside the loop.vals.transmogrify();}
Class, Package, and Method Naming and Capitalization
Guidelines
Classes begin with a capital letter.
Packages are all lower case.
Methods begin with a lower case letter.
Multi-word identifiers are internally capitalized in methods (CamelCase).