The code style is partly enforced by checkstyle rules
and could be summarized by the following points:
-
Indent by tabs, not spaces. The automated formatting provided by the Eclipse IDE is often used.
-
Lines should not be longer than 100-110 characters, although only a limit of 130 is enforced by
checkstyle. In justified cases (e.g.importstatements) it is acceptable to configure exclusions insuppressions.xml. -
Avoid trailing whitespace except for empty lines in Javadoc comments.
-
if-elseblocks should always use curly braces, even if a single line of code is involved. -
Long, descriptive variable names are preferred.
-
Add comments to explain what the code is trying to do, but avoiding useless prose that just mimics the code, like "check if foo is larger than 1" as a comment to
if (foo > 1). -
Public and protected methods should have documentation comments.
-
Code readability should not be sacrificed for compactness, unless there are obvious gains and the trade-off can be justified. For example,
i++; foo(i);is preferable tofoo(++i);except in conditional expressions. -
Classes and methods should have the minimum visibility that they require. A method should not have
protectedvisibility when being package-visible could be enough, unless subclasses in other packages would naturally extend it. For complex package-level or inner classes, it is acceptable to haveprotectedmethods as a mean to document which ones are intended to be overridden by other classes. (In that case, protected methods do not appear in the Javadocs and therefore are not part of the API) -
Local variables and the
finalkeyword: in Java, final local variables are automatically detected by the compiler, so thefinalkeyword is unnecessary and only adds clutter. However, in some cases it is good to explicitly mark a local variable asfinal: when it may be confused with a non-final one, when setting a literal value, or when used in a final-only context (e.g. lambdas).
The old codebase contains several chunks of commented code; those comments are often an indication that either that area or a related part of the codebase could be buggy. Please do not remove them unless you have understood why the commented code is there, and determined that there is no issue in doing it.
At this project's initial code clean-up, in at least one case where commented code was removed, the surrounding non-commented part had to be modified as well, because there was a bug there.
It is acceptable, however, to replace commented code with an informational task comment, if that does not lead to the loss of information. For example:
// FIXME: parameter 'foo' may not be correctly initialised herePull requests should not contain newly commented code.
Please see 'Copyright and attribution' in CONTRIBUTING.
For any comment or suggestion about the code style, please open a discussion.