Skip to content

Style Guide

Stuart Scott edited this page Oct 20, 2015 · 3 revisions

File Structure

Each source code file contains:

  • Header, see the Wiki
  • Package statement
  • Import statements
  • Exactly one top-level class

Exactly one blank line separates each of these sections that is present.

General Code

Imports

Import only what you use, in alphabetical order.

  • No unused imports.
  • No asterisk imports.
  • No static imports.
  • Separate blocks of similar imports with a blank line.

Spacing

A single space to separate parts, do not indent the initialization to line-up with another declaration on an adjacent line.

Good

int c = a + b;

for (int i = 0; i < count; i++) {
    start(i);
}

Bad

int    c       = a + b;
int    diff    = a - b;
String message = "Hello World!";

for(int i=0;i<count;i++) {
    start(i);
}

Braces

Always use Kernighan and Ritchie style ("Egyptian brackets") for braces. The opening brace should appear as the last character on the line the class/method/control structure was started. The closing brace should appear on its own line, unless in the case of an "else" or "else if". Empty blocks maybe opened and closed on the same line.

Good

if (test) {
    foo();
}

if (test) {
    foo();
} else {
    bar();
}

while (waiting) {}

for (int i = 0; i < count; i++) {
    start(i);
}

Bad

if (test)
    foo();

if (test)
{
    foo();
}
else
{
    bar();
}

for (int i = 0; i < count; i++) start(i);

Indentation

Indentation is 4 spaces

Line Length

Lines should be limited to 100 chars, although sometimes breaking a line can make it less readable so use your best judgement. The second part of a broken line is indented once more than the first part. Never break imports, URLs or comments that are expected to be copied-and-pasted.

Clone this wiki locally