-
Notifications
You must be signed in to change notification settings - Fork 1
Style Guide
Each Java source code should contain:
- A Header (see Wiki)
- A package declaration
- Import statements
- Exactly one top-level class
Exactly one blank line separates each of these sections that is present.
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.
A single space to separate parts, do not indent the initialization to line-up with another declaration on an adjacent line - the entire file should not need to be reformatted every time someone make a change just to line things up.
Good
int c = a + b;
int diff = a - b;
String message = "Hello World!";
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);
}
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 is 4 spaces
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.
Copyright (C) 2015 OpenFlame Project. Licensed under the GNU General Public License.