This is Salling Group’s official Java style guide.
The Salling Group Java style is forked from the Google Java style. This document is an extension of the Google Java style that documents deviations and additional rules.
To the extent possible, the style guide is enforced by
sg-java-format.
When this style guide, sg-java-format, and users disagree, what happens?
-
sg-java-formattakes precedence, as the enforcer of the style guide. Ifsg-java-formatviolates this style guide, one or the other should be changed accordingly. -
This style guide prescribes canonical behavior. Behavior not explicitly documented here is inherited from the Google Java style. If the behavior falls under
sg-java-format's purview (e.g. code formatting) the formatter must implement the specification as prescribed. For other rules the style guide is unconcerned with enforcement. -
If a user disagrees with either the style guide or
sg-java-formatthey may propose a style change.
If a proposed change to the style guide conflicts with sg-java-format, the
proposal cannot proceed until an accompanying change to sg-java-format is
submitted. Such a proposal can be rejected if the resulting code change is
deemed too complex.
This section lists rules that differ from, or do not exist in, the Google Java style. Refer to Section 1.1, “Settling disputes” for precedence rules.
Internal code should not have copyright headers. Copyright is implicitly all-rights-reserved. These headers are purely noise and maintaining them busywork.
Open source code may have copyright headers but we strongly advise leaving them out. Instead, the (mandatory) project level license implicitly applies to all source files not explicitly exempted.
|
ℹ️
|
Historically, a major motivating factor for copyright headers is that blindly copying individual source files will automatically preserve the original copyright. In reality, vanishingly little can be done to prevent or correct such misbehavior. |
Each time a new block or block-like construct is opened, the indent increases by four spaces. When the block ends, the indent returns to the previous indent level.
The choice of four spaces is a wildly popular one both in Java projects and in other ecosystems so users are likely to find it familiar and comfortable. It produces visually distinct indentation levels and scales pragmatically in the number of people.
|
ℹ️
|
The tab character is inappropriate for indentation:
|
Java code has a column limit of 80 characters.
|
ℹ️
|
It has become popular to think that especially Java code cannot be written
cleanly with a column limit of 80, or that, with 24" monitors, we should not
have to adhere to such “archaic restrictions”. In reality it is perfectly
easy to write clean Java code while adhering to a column limit of 80, and such
a limit is not archaic but rather pragmatic with respect to reading and version
control: shorter lines are easier to skim and less likely to change. OpenJDK
itself is a good example of this limit: as of |
|
ℹ️
|
A more ideal limit would be “80-ish” but that’s not enforceable with tooling, and exceptions scale much worse than absolutes. |
Study Oracle’s official How to Write Doc Comments for the Javadoc Tool guide. It’s old so it sports some dated advice, and written for a high-profile library so it has very high requirements, but it effectively established fairly good conventions.