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;
private static float sumofsquares = 0;
private static float sum_of_squares = 0;
private static float qw = 0; 

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){
  System.out.println("Hello"+" "+"World");
}

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)
{
  vals.insertElementAt(new Float (args[i]), i);
// Transmogrify is incremental and more efficient inside the loop.
  vals.transmogrify();
  }

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;
public class Meanstandarddeviation
private Vector GetNewVector(Vector oldVector) {

Program Modules

Guidelines

Other References

Website

How to Write Doc Comments for the Javadoc Tool