Java Programming Style Guide

Formatting Conventions

Formatting refers to the indentation, alignment, and use of white space to lay out your program to increase its readability by others.

Consistency is the key to producing readable code. While you can argue the merits of 3 versus 4 spaces of indentation, placement of curly braces, etc., the real key is to adopt a formatting style and keep to it.

Organizations adopt a standard for formatting to increase programmer productivity by reducing variations and trivial decisions among teams of programmers. By following formatting standards, programmers do not need to individually decide on the relatively trivial matters of formatting. Instead, they can spend their time and energy on the harder problems of the program solution.


Indentation:

Use four spaces for indentation to indicate nesting of control structures.

Curly Braces:

Put the opening curly brace for a block of code in a separate line at the beginning of the compound statement.

if (condition)
{
statements;
}
else if (condition)
{
statements;
}
else
{
statements;
}

White Space:

Use blank lines and spaces to improve the readability of your code.

Use blank lines to separate chunks of program code. Chunks are logical groups of program statements, usually proceeded with a single-line summary comment. Use one blank line before every program chunk. Use two blank lines before the start of each new method within a class.

Use one blank space on both sides of operator symbols, after commas in argument lists, and after semicolons in for statements.

Line Length:

Avoid lines longer than 80 characters When an expression will not fit on a single line of 80 characters, break it according to these general principles:

someMethod(longExpression1, longExpression2, longExpression3, 
longExpression4, longExpression5);

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