Guidelines
Example
private static float sumOfSquares = 0;
Counter Example
private static float Sum = 0; // Doesn't start with lower case letter
private static float sumofsquares = 0; // Not internally capitalized
private static float sum_of_squares = 0; // Uses underscores
private static float qw = 0; // Not descriptive, just adjacent characters.
Guidelines
Example
/**
* Haiku, prints a haiku.
* @author Suzuki, Susan
*/
public class Haiku {
// ...
}
Guidelines
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
*/
public boolean printWord(String word) throws SpacesFoundException {
// ...
}
Guidelines
Example
/** Toggles between Frame and NoFrame mode. */
public boolean frameMode = true;
Guidelines
Example
// Do a 3D transmogrification on the matrix.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
for (int k = 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 matrix
for (int i = 0; i < matrix.length; i++) {
// ...
}
Guidelines
Example
public static void main(String[] arg) {
System.out.println("Hello" + " " + "World");
}
public void foo() {
// ...
}
Guidelines
Example
public static void main(String[] arg) {
System.out.println("Hello" + " " + "World");
}
Counter Example
public static void main(String[] arg){ // No space before '{'
System.out.println("Hello"+" "+"World"); // No spaces around '+'
}
Guidelines
Example
for (int i=0; i < args.length; i = i + 1) {
vals.insertElementAt(new Float (args[i]), i);
// Transmogrify is incremental and more efficient inside the loop.
vals.transmogrify();
}
Counter Example
for (int i=0; i < args.length; i = i + 1)
{ // '{' on own line
vals.insertElementAt(new Float (args[i]), i);
// Transmogrify is incremental and more efficient inside the loop.
vals.transmogrify();
} // should not be indented, should align with 'for'
Guidelines
Example
package foo.bar.baz;
public class MeanStandardDeviation
private Vector getNewVector(Vector oldVector) {
Counter Example
package Foo.Bar.Baz; // Packages not lower case
public class Meanstandarddeviation // Not internally capitalized
private Vector GetNewVector(Vector oldVector) { // First letter not lower case
Guidelines
Website