Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This

...

page

...

documents

...

the

...

standard

...

adopted

...

for

...

Java

...

code

...

in

...

the

...

Qpid

...

project.

...

All

...

committers

...

are

...

expected

...

to

...

follow

...

these

...

standards;

...

checkstyle

...

or

...

similar

...

is

...

used

...

to

...

check

...

compliance.

Table of Contents

Executive Summary

The main things for layout purposes in the standard are:

  • Indent using four spaces. No tabs.
  • braces always go on new lines, e.g.
Code Block


h3. Executive Summary

The main things in the standard are:

* Indent using four spaces. *No tabs*.
* braces always go on new lines, e.g.

{code}
if (x == 5)
{
    System.out.println("Hello");
}
{code}

rather

...

than

{
Code Block
}
if (x == 5} {
    System.out.println("Hello");
}
{code}
* Always add 
  • Always add braces,

...

  • e.g.

...

  • Code Block

...

  • if (x == 5)
    {
        System.out.println("Hello");
    }
    

...

rather

...

than

{
Code Block
}
if (x == 5}
    System.out.println("Hello");
{code}
* Fields prefixed with 
  • Fields prefixed with underscores,

...

  • e.g.

...

  • _messageCount

...

  • Spaces

...

  • after

...

  • keywords

...

  • but

...

  • no

...

  • spaces

...

  • either

...

  • before

...

  • or

...

  • after

...

  • parentheses

...

  • in

...

  • method

...

  • calls,

...

  • e.g.

...

  • Code Block

...

  • if (x == 5)
    

...

  • rather

...

  • than

...

  • Code Block

...

  • if(x==5)
    

...

  • but

    Code Block
    foo.bar(4, 5)
    

...

  • rather

...

  • than

...

  • Code Block

...

  • foo.bar( 4, 5 )
    

...

Details

Introduction

This document describes two types of coding standard:

1. Mandatory standards must be followed at all times.
2. Recommended standards should in general be followed but in particular cases may be omitted where the programmer feels that there is a good reason to do so.

Code that does not adhere to mandatory standards will not pass the automated checks (or a code review if the guideline is not stylistic).

Source files

This section defines the general rules associated with the contents of a Java source file and the order in which the each part should be presented. No rules on programming style, naming conventions or indentation are given here.

  1. Java source files must have a ".java" suffix (this will be enforced by the compiler) [mandatory].
  2. The basename of a Java source file must be the same as the public class defined therein (this will be enforced by the compiler) [mandatory].
  3. Only one class should be defined per source file (except for inner classes and one-shot uses where the non-public class cannot conceivably be used outside of its context) [mandatory].
  4. Source files should not exceed 1500 lines [recommended].
  5. No line in a source file should exceed 120 characters [mandatory].
  6. The sections of a source file should be presented in the following order [mandatory]:
  • File information comment (see rule 7 below).
  • Package name (see rules 1 to 3 in the section 2.1 above and rule 8 below).
  • Imports (see rules 9 to 10 below).
  • Other class definitions.
  • Public class definition.
  1. Do not use automatically expanded log or revision number provided by your source code management system unless it provides a facility to avoid "false conflicts" when doing merges due simply to revision number changes (which happens, for example, with cvs when branches are used). [mandatory]
  2. Every class that is to be released must be a member of a package [mandatory].
    Rationale: classes that are not explicitly put in a package are placed in the unnamed package by the compiler. Therefore as the classes from many developers will be being placed in the same package the likelihood of a name clash is greatly increased.
  3. All class imports from the same package should be grouped together. A single blank line should separate imports from different packages [recommended].
  4. Use javadoc tags and use HTML mark-up to enhance the readability of the output files [mandatory].

Java Elements

This section gives advice on coding the various elements of the Java programming language.

Class definitions

This section gives guidelines for class and interface definitions in Java. The term class in this section is used more broadly to mean class and interface:

  1. Class names should start with a capital letter with every subsequent word capitalised, for example: DataProcessor [mandatory].
  2. All classes should be preceded by a javadoc comment describing the purpose of the class [recommended].
  3. Class-level javadoc comments should specify the thread-safety of the class [recommended].
  4. The name of exception classes should end in the word exception, for example: UnknownMungeException [mandatory].
  5. Class names should in general not be overloaded. For example, defining a class "com.foo.bar.String"

...

  1. should

...

  1. be

...

  1. avoided

...

  1. as

...

  1. there

...

  1. is

...

  1. already

...

  1. a

...

  1. class

...

  1. "java.lang.String"

...

  1. [recommended

...

  1. ].

...


  1. Rationale:

...

  1. adhering

...

  1. to

...

  1. this

...

  1. rule

...

  1. reduces

...

  1. the

...

  1. likelihood

...

  1. of

...

  1. confusion

...

  1. and

...

  1. means

...

  1. that

...

  1. the

...

  1. use

...

  1. of

...

  1. fully

...

  1. qualified

...

  1. class

...

  1. names

...

  1. should

...

  1. not

...

  1. be

...

  1. required.

...

  1. The

...

  1. definition

...

  1. of

...

  1. the

...

  1. primary

...

  1. class

...

  1. (i.e.

...

  1. the

...

  1. class

...

  1. with

...

  1. the

...

  1. same

...

  1. name

...

  1. as

...

  1. the

...

  1. java

...

  1. file)

...

  1. should

...

  1. start

...

  1. in

...

  1. column

...

  1. 0

...

  1. of

...

  1. the

...

  1. source

...

  1. file.

...

  1. Inner

...

  1. class

...

  1. definitions

...

  1. should

...

  1. be

...

  1. indented

...

  1. 4

...

  1. spaces

...

  1. more

...

  1. than

...

  1. their

...

  1. enclosing

...

  1. class

...

  1. [mandatory

...

  1. ].

...

  1. Declare

...

  1. a

...

  1. class

...

  1. as

...

  1. final

...

  1. only

...

  1. if

...

  1. specialisation

...

  1. will

...

  1. never

...

  1. be

...

  1. required

...

  1. and

...

  1. improved

...

  1. performance

...

  1. is

...

  1. essential.

...

  1. With

...

  1. modern

...

  1. JVMs

...

  1. there

...

  1. in

...

  1. fact

...

  1. may

...

  1. be

...

  1. no

...

  1. performance

...

  1. advantage.

...

  1. Warning:

...

  1. use

...

  1. of

...

  1. final

...

  1. limits

...

  1. code

...

  1. reuse

...

  1. [mandatory

...

  1. ].

...

  1. For

...

  1. all

...

  1. but

...

  1. simplest

...

  1. classes

...

  1. the

...

  1. following

...

  1. methods

...

  1. should

...

  1. have

...

  1. useful

...

  1. definitions

...

  1. [recommended

...

  1. ]:

...

  1. Code Block

...

  1. public boolean equals(Object obj)
    public int hashCode()
    public String toString()
    

...

  1. The order of presentation of the sections in a class should be [mandatory]:
  • Variables
  • Methods

    Variables

    This section gives guidelines for class and instance variable definitions in Java. In this section if a rule uses the term variable rather than instance variable or class variable, then the rule applies to both types of variable.
  1. The order of presentation of variables in a class definition should be [recommended]:
  • private, protected, public: static final variables (aka constant class variables).
  • private, protected, public: static variables (aka class variables).
  • private, protected, public: final variables (aka constant instance variables).
  • private, protected, public: variables (aka instance variables).
    It should be noted that as javadoc will automatically order variables in a consistent manner, rigid adherence to this rule is not necessary.
  1. Variable modifiers should be presented in the following order: static, final, transient, volatile [mandatory].
  2. The names of static final variables should be upper case with subsequent words prefixed with an underscore [mandatory]. For example:

    Code Block
    public static final int NOT_FOUND = -1;
    

...

  1. When a subclass refers to a static final variable defined in a parent class, access should be qualified by specifying the defining class name [mandatory]. For example: use ParentClass.MAX rather than MAX.
  2. The names of variables (other that static final) should start with a lower case letter. Any words that are contained in the rest of the variable name should be capitalised [mandatory]. For example:

    Code Block
    String name;
    String[] childrensNames;
    

...

  1. Class and instance variables must be prefixed with an underscore (_) [mandatory].
  2. Variables must not be named using the so-called Hungarian notation [mandatory]. For example:

    Code Block
    int nCount = 4; // not allowed
    

...

  1. Only one variable may be defined per line [mandatory].
  2. Variable declarations should be indented 4 spaces more than their enclosing class [mandatory].
  3. All variables should be preceded by a javadoc comment that specifies what the variable is for, where it is used and so forth. The comment should be of the following form and be indented to the same level as the variable it refers to [recommended]
  4. Never declare instance variables as public unless the class is effectively a "struct" [mandatory].
  5. Never give a variable the same name as a variable in a superclass [mandatory].
  6. Ensure that all non-private class variables have sensible values even if no instances have been created (use static initialisers if necessary, i.e. "static { ... }") [mandatory].
    Rationale: prevents other objects accessing fields with undefined/unexpected values.

    Methods

    This section gives guidelines for class and instance method definitions in Java. In this section if a rule uses the term method rather than instance method or class method, then the rule applies to both types of method.
  7. Constructors and finalize methods should follow immediately after the variable declarations [mandatory].
  8. Do not call non-final methods from constructors. This can lead to unexpected results when the class is subclassed. If you must call non-final methods from constructors, document this in the constructor's javadoc [mandatory]. Note that private implies final.
  9. Methods that are associated with the same area of functionality should be physically close to one another [recommended].
  10. After grouping by functionality, methods should be presented in the following order [recommended]:
  • private, protected, public: static methods.
  • private, protected, public: instance methods.
    It should be noted that as javadoc will automatically order methods in a consistent manner, rigid adherence to this rule is not necessary.
  1. Method modifiers should be presented in the following order: abstract, static, final., synchronized [mandatory]
  2. When a synchronized method is overloaded, it should be explicitly synchronized in the subclass [recommended].
  3. Method names should start with a lower case letter with all subsequent words being capitalised [mandatory]. For example:

    Code Block
    protected int resize(int newSize)
    protected void addContentsTo(Container destinationContainer)
    

...

  1. Methods which get and set values should be named as follows [mandatory]:

    Code Block
    Type getVariableName()
    void setVariableName(Type newValue)
    

...

  1. Exceptions

...

  1. should

...

  1. be

...

  1. used

...

  1. to

...

  1. report

...

  1. any

...

  1. failure

...

  1. to

...

  1. get

...

  1. or

...

  1. set

...

  1. a

...

  1. value.

...

  1. The

...

  1. "@param"

...

  1. description

...

  1. should

...

  1. detail

...

  1. any

...

  1. assumptions

...

  1. made

...

  1. by

...

  1. the

...

  1. implementation,

...

  1. for

...

  1. example:

...

  1. "Specifying

...

  1. a

...

  1. null

...

  1. value

...

  1. will

...

  1. cause

...

  1. an

...

  1. error

...

  1. to

...

  1. be

...

  1. reported".

...

  1. Method

...

  1. definitions

...

  1. should

...

  1. be

...

  1. indented

...

  1. 4

...

  1. spaces

...

  1. more

...

  1. than

...

  1. their

...

  1. enclosing

...

  1. class

...

  1. [mandatory

...

  1. ].

...

  1. All non-private methods should be preceded by a javadoc comment specifying what the method is for, detailing all arguments, returns and possible exceptions [mandatory]
  2. The braces associated with a method should be on a line on their own and be indented to the same level as the method [mandatory]. For example:

    Code Block
    public void munge()
    {
        int i;
        // method definition omitted...
    }
    

...

  1. The body of a method should be indented 4 columns further that the opening and closing braces associated with it [mandatory]. See the above rule for an example.
  2. When declaring and calling methods there should be no white space before or after the parenthesis [mandatory].
  3. In argument lists there should be no white space before a comma, and only a single space (or newline) after it [mandatory]. For example:

    Code Block
    public void munge(int depth, String name)
    {
        if (depth > 0)
        {
            munge(depth - 1, name);
        }
        // do something
    }
    

...

  1. Wherever reasonable define a default constructor (i.e.

...

  1. one

...

  1. that

...

  1. takes

...

  1. no

...

  1. arguments)

...

  1. so

...

  1. that

...

  1. Class.newInstance()

...

  1. may

...

  1. be

...

  1. used

...

  1. [recommended

...

  1. ].

...

  1. If

...

  1. an

...

  1. instance

...

  1. which

...

  1. was

...

  1. created

...

  1. by

...

  1. default

...

  1. construction

...

  1. could

...

  1. be

...

  1. used

...

  1. until

...

  1. further

...

  1. initialisation

...

  1. has

...

  1. been

...

  1. performed,

...

  1. then

...

  1. all

...

  1. unserviceable

...

  1. requests

...

  1. should

...

  1. cause

...

  1. a

...

  1. runtime

...

  1. exception

...

  1. to

...

  1. be

...

  1. thrown.

...

  1. The

...

  1. method

...

  1. public

...

  1. static

...

  1. void

...

  1. main()

...

  1. should

...

  1. not

...

  1. be

...

  1. used

...

  1. for

...

  1. test

...

  1. purposes.

...

  1. Instead

...

  1. a

...

  1. test/demo

...

  1. program

...

  1. should

...

  1. be

...

  1. supplied

...

  1. separately.

...

  1. [mandatory

...

  1. ].

...

  1. Public

...

  1. access

...

  1. methods

...

  1. (i.e.

...

  1. methods

...

  1. that

...

  1. get

...

  1. and

...

  1. set

...

  1. attributes)

...

  1. should

...

  1. only

...

  1. be

...

  1. supplied

...

  1. when

...

  1. required

...

  1. [mandatory

...

  1. ].

...

  1. If

...

  1. an

...

  1. instance

...

  1. method

...

  1. has

...

  1. no

...

  1. natural

...

  1. return

...

  1. value,

...

  1. declare

...

  1. it

...

  1. as

...

  1. void

...

  1. rather

...

  1. than

...

  1. using

...

  1. the

...

  1. "return

...

  1. this;"

...

  1. convention

...

  1. [mandatory

...

  1. ].

...

  1. Ensure

...

  1. that

...

  1. non-private

...

  1. static

...

  1. methods

...

  1. behave

...

  1. sensibly

...

  1. if

...

  1. no

...

  1. instances

...

  1. of

...

  1. the

...

  1. defining

...

  1. class

...

  1. have

...

  1. been

...

  1. created

...

  1. [mandatory

...

  1. ].

...

  1. Expressions

    This section defines the rules to be used for Java expressions:
  2. Unary operators should not be separated from their operand by white space [mandatory].
  3. Embedded ++ or – operators should only be used when it improves code clarity [recommended]. This is rare.
  4. Extra parenthesis should be used in expressions to improve their clarity [recommended].
  5. The logical expression operand of the "?:" (ternary) operator must be enclosed in parenthesis. If the other operands are also expressions then they should also be enclosed in parenthesis [mandatory]. For example:

    Code Block
    biggest = (a > b) ? a : b;
    complex = (a + b > 100) ? (100 * c) : (10 * d);
    

...

  1. Nested "?:"

...

  1. (ternary)

...

  1. operators

...

  1. can

...

  1. be

...

  1. confusing

...

  1. and

...

  1. should

...

  1. be

...

  1. avoided

...

  1. [mandatory

...

  1. ].

...

  1. Use

...

  1. of

...

  1. the

...

  1. binary

...

  1. ","

...

  1. operator

...

  1. (the

...

  1. comma

...

  1. operator)

...

  1. should

...

  1. be

...

  1. avoided

...

  1. [mandatory

...

  1. ].

...

  1. Putting

...

  1. all

...

  1. the

...

  1. work

...

  1. of

...

  1. a

...

  1. for

...

  1. loop

...

  1. on

...

  1. a

...

  1. single

...

  1. line

...

  1. is

...

  1. not

...

  1. a

...

  1. sign

...

  1. of

...

  1. great

...

  1. wisdom

...

  1. and

...

  1. talent.

...

  1. If

...

  1. an

...

  1. expression

...

  1. is

...

  1. too

...

  1. long

...

  1. for

...

  1. a

...

  1. line

...

  1. (i.e.

...

  1. extends

...

  1. beyond

...

  1. column

...

  1. 119)

...

  1. then

...

  1. it

...

  1. should

...

  1. be

...

  1. split

...

  1. after

...

  1. the

...

  1. lowest

...

  1. precedence

...

  1. operator

...

  1. near

...

  1. the

...

  1. break

...

  1. [mandatory

...

  1. ].

...

  1. For

...

  1. example:

...

  1. Code Block

...

  1. if ((state == NEED_TO_REPLY) ||
        (state == REPLY_ACK_TIMEOUT))
    {
        // (re)send the reply and enter state WAITING_FOR_REPLY_ACK
    }
    

...

  1. Furthermore

...

  1. if

...

  1. an

...

  1. expression

...

  1. requires

...

  1. to

...

  1. be

...

  1. split

...

  1. more

...

  1. than

...

  1. once,

...

  1. then

...

  1. the

...

  1. split

...

  1. should

...

  1. occur

...

  1. at

...

  1. the

...

  1. same

...

  1. logical

...

  1. level

...

  1. if

...

  1. possible.

...

  1. All

...

  1. binary

...

  1. and

...

  1. ternary

...

  1. operators

...

  1. (exception

...

  1. for

...

  1. ".")

...

  1. should

...

  1. be

...

  1. separated

...

  1. from

...

  1. their

...

  1. operands

...

  1. by

...

  1. a

...

  1. space

...

  1. [mandatory

...

  1. ].

...

  1. Statements

Simple Statements

This section defines the general rules for simple Java statements:

  1. There must only be one statement per line [mandatory].
  2. In general local variables should be named in a similar manner to instance variables [recommended].
  3. More than one temporary variable may be declared on a single line provided no initialisers are used [mandatory]. For example:

    Code Block
    int j, k = 10, l;  // Incorrect!
    int j, l;          // Correct
    int k = 10;
    

...

  1. A null body for a while, for, if, etc. should be documented so that it is clearly intentional [mandatory].
  2. Keywords that are followed by a parenthesised expression (such as while, if, etc) should be separated from the open bracket by a single space [mandatory]. For example:

    Code Block
    if (a > b)
    {
        munge();
    }
    

...

  1. In method calls,

...

  1. there

...

  1. should

...

  1. be

...

  1. no

...

  1. spaces

...

  1. before

...

  1. or

...

  1. after

...

  1. the

...

  1. parentheses

...

  1. [mandatory

...

  1. ].

...

  1. For

...

  1. example:

...

  1. Code Block

...

  1. munge (a, 10);    // Incorrect!
    munge(a, 10);     // Correct.
    

...

Compound Statements

This section defines the general rules associated with compound statements in Java:

  1. The body of a compound statement should be indented by 4 spaces more than the enclosing braces [mandatory]. See the following rule for an example.
  2. The braces associated with a compound statement should be on their own line and be indented to the same level as the surrounding code [mandatory]. For example:

    Code Block
    if ((length >= LEN_BOX) && (width >= WID_BOX))
    {
        int i;
        // Statements omitted...
    }
    

...

  1. If the opening and closing braces associated with a compound statement are further than 20 lines apart then the closing brace should annotated as follows [mandatory]:

    Code Block
    for (int j = 0; j < SIZE; j++)
    {
        lotsOfCode();
    } // end for
    

...

  1. All statements associated with an if or if-else

...

  1. statement

...

  1. should

...

  1. be

...

  1. made

...

  1. compound

...

  1. by

...

  1. the

...

  1. use

...

  1. of

...

  1. braces

...

  1. [mandatory

...

  1. ].

...

  1. For

...

  1. example:

...

  1. Code Block

...

  1. if (a > b)
    {
        statement();
    }
    else
    {
        statement1();
        statement2();
    }
    

...

  1. The case labels in a switch statement should be on their own line and indented by a further 4 spaces. The statements associated with the label should be indented by 4 columns more than the label and not be enclosed in a compound statement. [mandatory]. For example:

    Code Block
    switch (tState)
    {
        case NOT_RUNNING:
            start();
            break;
    
        case RUNNING:
        default:
            monitor();
            break;
    }
    

...

  1. In switch statements - the statements associated with all cases should terminate with a statement which explicitly determines the flow of control, for example break [recommended].
  2. In switch statements - fall through should be avoided wherever possible, however if it is unavoidable it must be commented with "// FALLTHROUGH" [mandatory].
  3. In switch statements - a default case must be present and should always be the last case [mandatory].

    General

    This section gives general rules to be followed when programming in Java:
  4. When comparing objects for equivalence use the method equals() and not the == operator. The only exceptions to this are static final objects that are being used as constants and interned Strings [mandatory].
  5. In general labelled break and continue statements should be avoided [recommended]. This is due to the complex flow of control, especially when used with try/finally blocks.
  6. Unless some aspect of an algorithm relies on it, then loops count forward [mandatory]. For example:

    Code Block
    for (int j = 0; j < size; j++)
    {
        // Do something interesting
    }
    

...

  1. Use local variables in loops [recommended]. For example:

    Code Block
    ArrayList clone = (ArrayList)listeners.clone();
    final int size = clone.size();
    for (int j = 0; j < size; j++)
    {
        System.out.println(clone.elementAt(j));
    }
    

...

  1. Anonymous inner classes should define no instance variables and be limited to three single line methods. Inner classes that declare instance variables or have more complex methods should be named [mandatory].
  2. Use final local variables where possible to help avoid errors in code [recommended]. For example:

    Code Block
    public void foo()
    {
        final int x = dataSource.getCount();
        // do things with x
        // ...

...

  1. 
    }
    
  2. To indicate that further work is intended on a section of code, add a comment prefixed by "TODO" explaining what needs to be done and why [recommended].
  3. If code is so incomplete that executing it would lead to incorrect or confusing results, throw UnsupportedOperationException with an explanatory message [mandatory].

Exceptions

This section gives general guidance on the use of exceptions when programming in Java.

  1. try/catch blocks should be laid out like any other compound statement [mandatory]. For example:

    Code Block
    try
    {
        String str = someStrings[specifiedIndex];
    }
    catch (IndexOutOfBoundsException ex)
    {
        // The user specified an incorrect index, better take
        // some remedial

...

  1.  action.
    

...

  1. }
    

    4.

...

  1. When

...

  1. an

...

  1. exception

...

  1. is

...

  1. caught

...

  1. but

...

  1. ignored

...

  1. then

...

  1. a

...

  1. comment

...

  1. should

...

  1. be

...

  1. supplied

...

  1. explaining

...

  1. the

...

  1. rationale (note that this rule includes InterruptedException, which should almost never be ignored) [mandatory].

...

  1. For

...

  1. example:

...

  1. Code Block

...

  1. try
    {
        propertySet.setProperty("thingy", new Integer(10));
    }
    catch (UnknownPropertyException ignore)
    {
        // This exception will never occur as "thingy" definitely exists
    }

...

  1. 
    
  2. All exceptions that are likely to be thrown by a method should be documented, except if they are runtime exceptions (note: the compiler will not enforce catch blocks for runtimes even if they are mentioned in the throws clause) [mandatory]. For example:

    Code Block
    /* Comment snippet:
     * @exception IllegalValueException Thrown if values is null or

...

  1. 
     *     any of the integers it contains is null.
     */
    private Integer sum(Integer[] values) throws IllegalValueException
    

...

IDE Code Style

There is a code style file for the IntelliJ IDE located at /etc/IntelliJ_Qpid_Style.xml in the source tree. All Qpid Java developers should use this or an equivalent style for their IDE.