Wednesday, March 4, 2009

Java coding standards

3.1 Java Source Files

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

Java source files have the following ordering:

3.1.1 Beginning Comments

All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:

/*  * Classname  *   * Version information  *  * Date  *   * Copyright notice  */ 

3.1.2 Package and Import Statements

The first non-comment line of most Java source files is a package statement. After that, import statements can follow. For example:

package java.awt;  import java.awt.peer.CanvasPeer; 

Note: The first component of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

3.1.3 Class and Interface Declarations

The following table describes the parts of a class or interface declaration, in the order that they should appear. See "Java Source File Example" on page 19 for an example that includes comments.



Part of Class/Interface Declaration


Notes


1


Class/interface documentation comment (/**...*/)


See "Documentation Comments" on page 9 for information on what should be in this comment.


2


class or interface statement



3


Class/interface implementation comment (/*...*/), if necessary


This comment should contain any class-wide or interface-wide information that wasn't appropriate for the class/interface documentation comment.


4


Class (static) variables


First the public class variables, then the protected, then package level (no access modifier), and then the private.


5


Instance variables


First public, then protected, then package level (no access modifier), and then private.


6


Constructors



7


Methods


These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier.

4 - Indentation

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

4.1 Line Length

Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters.

4.2 Wrapping Lines

When an expression will not fit on a single line, break it according to these general principles:

  • Break after a comma.
  • Break before an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

someMethod(longExpression1, longExpression2, longExpression3,          longExpression4, longExpression5);   var = someMethod1(longExpression1,                 someMethod2(longExpression2,                         longExpression3));  

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.

longName1 = longName2 * (longName3 + longName4 - longName5)            + 4 * longname6; // PREFER  longName1 = longName2 * (longName3 + longName4                        - longName5) + 4 * longname6; // AVOID  

Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.

//CONVENTIONAL INDENTATION someMethod(int anArg, Object anotherArg, String yetAnotherArg,            Object andStillAnother) {     ... }  //INDENT 8 SPACES TO AVOID VERY DEEP INDENTS private static synchronized horkingLongMethodName(int anArg,         Object anotherArg, String yetAnotherArg,         Object andStillAnother) {     ... } 

Line wrapping for if statements should generally use the 8-space rule, since conventional (4 space) indentation makes seeing the body difficult. For example:

//DON'T USE THIS INDENTATION if ((condition1 && condition2)     || (condition3 && condition4)     ||!(condition5 && condition6)) { //BAD WRAPS     doSomethingAboutIt();            //MAKE THIS LINE EASY TO MISS }   //USE THIS INDENTATION INSTEAD if ((condition1 && condition2)         || (condition3 && condition4)         ||!(condition5 && condition6)) {     doSomethingAboutIt(); }   //OR USE THIS if ((condition1 && condition2) || (condition3 && condition4)         ||!(condition5 && condition6)) {     doSomethingAboutIt(); }  

Here are three acceptable ways to format ternary expressions:

alpha = (aLongBooleanExpression) ? beta : gamma;    alpha = (aLongBooleanExpression) ? beta                                  : gamma;    alpha = (aLongBooleanExpression)         ? beta          : gamma;