-
Notifications
You must be signed in to change notification settings - Fork 40
Java Coding Conventions
Mark Woon edited this page Oct 27, 2021
·
3 revisions
Recommended reading: Java For Small Teams.
We mainly follow Google's Java style guide.
Formatting:
- Spaces, not tabs.
- Block indentation is 2 spaces.
- K&R style
- Braces are not optional
Example:
public String doSomething(String str, Iterator itr) throws Exception {
if (str == null) {
complain("str is null!");
} else {
System.out.println(str);
}
while (itr.hasNext()) {
String val = (String)itr.next();
System.out.println(val);
}
return "Done!";
}
Not Optional: Make sure you put braces ({}) around all conditionals (i.e. if, else, while, for, etc.), no matter how short. This makes it much harder to make any scoping mistakes.
Naming Conventions:
- For class names, use upper camel case.
- For method and variable names, use lower camel case.
- In class and method names, acronyms and contractions should only have the first letter capitalized (eg. Db, Kb, Url). This is in keeping with the camel casing convention.
- Longer, more descriptive names are better than short cryptic names.
- Public static final variables should be in all caps.
- One variable per declaration (e.g. no
int a, b;
)
Import order:
-
java
imports -
javax
imports - all non-static imports, in ASCII order
- blank line
- all static imports
Use Checker Framework's @Nullable
/@NonNull
annotations directly in the source.
Internally, we use SLF4J for all our logging needs. This, however, is just an interface. The actual logging implementation we use is Logback.
All code should be documented, and you should use proper Javadoc formatting. If in doubt, consult the Javadoc Website.