Skip to content
Michael[tm] Smith edited this page Apr 12, 2015 · 3 revisions

Formatting

Indent by two spaces.

Put spaces around binary operators:

x = y + z;
if (foo == null) {
  ...
}

Opening braces are on the same line as the keyword, and closing braces are on a line by themselves:

if (foo()) {
  bar();
  baz();
}
else {
  baz();
  bar();
}
try {
  foo();
}
catch (FooException e) {
}

except for do/while, where the closing brace is on the same line as the while.

do {
  foo();
} while (bar);

Don't parenthesize return:

return foo.bar();

One-line if/else/for clauses typically aren't enclosed in braces:

if (foo)
  bar();

(but you can use braces if you prefer).

Java version

Code should take advantage of Java 5 language features such as generics, as well as Java 5 APIs. In particular,

  • use HashMap not Hashtable (Hashtable has the overhead of synchronization)
  • use ArrayList not Vector (Vector has the overhead of synchronization)
  • use Iterator not Enumeration

Originally Jing and Trang were written to use Java 1.1 language features only, and not all code has yet been upgraded.