ICS Java Coding Standard

Basic Coding Rules to Follow

  1. Use descriptive and appropriate names for all identifiers (variables, method names, class names, constants, etc.).
  2. Comment confusing sections of code.
  3. Be neat.

Identifier Naming and Capitalization

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.

Comments: Classes

Guidelines

Example

/**
 * Haiku, prints a haiku.
 * @author Suzuki, Susan
 */
public class Haiku {
  // ...
}

Comments: Methods

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 {
  // ...
}

Comments: Public variables

Guidelines

Example

/** Toggles between Frame and NoFrame mode. */
public boolean frameMode = true;

Comments: In-line

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++) {
  // ...
}

Spacing: Between lines

Guidelines

Example

public static void main(String[] arg) {
  System.out.println("Hello" + " " + "World");
}


public void foo() {
  // ...
}

Spacing: Within lines

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 '+'
}

Indentation

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'

Class, Package, and Method Naming and Capitalization

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

Program Modules

Guidelines

Other References

Website

How to Write Doc Comments for the Javadoc Tool