SourceForge.net Logo

Transit Network logo

Programming Style Document

Version 1
26th November 2003
Matthew Wilson

 

In Java there are a few different ways that people arrange their code. I try to make my code as readable as possible, and so this is the style that is used for this project. This style guide is to outline the basic style of code that all programmers must use for this project.

Braces

Curly braces must go on a new line (both opening and closing).
Like this:

public class Example
{
    .
    .
    .
}

Not like these:

public void getValue() {
    return value;
}

public void getValue() { return value; }

public void getValue()
{ return value; }
top

Indents

All indents are the equivalent of four spaces, but should be represented in a source file as a tab character.
top

Naming

Class and method names must start with an upper case letter, variables start with a lower case letter. Classes, variables, and methods should not have underscores denoting spaces - they must be denoted by an upper case letter. Constants are all caps, with underscores denoting spaces.
Examples:

public class NewExample
{
    public static final int SCREEN_WIDTH = 1024;

    private int screenWidth;
}
top

Constants/Attributes

Constants are generally public, but can be private. Attributes must always be private.
top

Methods

Methods must always state whether they are public, private or protected - it must not be left out. There is no space between the method name and the first bracket. This applies to calling methods as well as defining them.
top

Control Statements

The style of control statements is slightly different from methods so as to distinguish the two. For all control statements, there is a space left between the statement and the first bracket. Furthermore, curly braces are always used, even if there is only one statement within the structure. The switch statement uses more indentation than is usually necessary:

switch (value)
{
    case 0 :
        System.out.print("Hello world!");
        break;
    case 1 :
    case 2 :
        System.exit(0);
        break;
    default :
        System.out.print("Can't you chose?");
}

For statements have spaces separating the arguments:

for (int i = 0; i < 22; i++)
{
    .
    .
}

Do and while are fairly straight-forward:

do
{
    .
    .
} while (value < 22);

while (value < 22)
{
    .
    .
}

Try..catch..finally blocks

Each keyword is on a new line:

try
{
    .
    .
}
catch (IOException e)
{
    .
}
catch (Exception e)
{
    .
}
finally
{
    System.exit(0);
}
top

Class structure

All classes must have the same general structure to them, outlined below:

Header - file name, class name, author, date, version.
Imports
Class title
Constants
Attributes
Constructors
Construction methods - methods that aid the constructors
Main method - must be before the other class methods
Get/set methods
Other class methods
Implementation methods - methods required by an interface
Listener methods - methods required by a Listener interface
End
top

Check for valid CSS Check for valid XHTML 1.0