-
Notifications
You must be signed in to change notification settings - Fork 71
Choose your code style
Style is an often overlooked but very critical attribute of writing. The style of writing directly impacts the readability and understandability of the end product.
There are 2 main java code styles
It is up to you which one you should choose. But the style should be chosen and the style should be the same for everyone.
To maintain the same standard config/ide/eclipse-java-google-style.xml
or intellij-java-google-style.xml
should be imported to your IDE.
Checkstyle should be configured based on the chosen standard.
With Java, one needs to rely on external tooling for linting. The most popular choice is:
However, Checkstyle will not auto-format code for you. For auto-formatting, consider, depending on your team preferences, any of these build plugins:
-
Spotless — For Gradle, use the
spotlessApply
task to reformat; for Maven use thespotless:apply
goal. The tool behind the plugin supports many source languages, not just Java. - Rewrite — General framework. for transforming source code with many extensions and plugins for different languages and frameworks. For Gradle, see Rewrite for Gradle with a focus on SUN style guides for Java; for Maven, see Rewrite for Maven with a focus on SUN style guides for Java
For your editor, consider the sample
.editorconfig
file in this project.
It is respected by IntelliJ and many other code editors.
(The sample uses 80-character line limits as IBM and Hollerith punch cards
intended, and helpful for speed readers of code.
A point worthy of team discussion.)
The demonstration projects assume checkstyle configuration at
config/checkstyle/checkstyle.xml
This is the default location for Gradle, and configured for Maven in the
project.
The Checkstyle configuration used is stock
sun_checks.xml
(this is SUN default style for Java) with the addition of support for
@SuppressWarnings(checkstyle:...)
. Note that this format is overly
aggressive for Javadocs, and needs tweaking for most projects. See comments
in build.gradle
about SUN vs Google styles for Java.
See the code repo for working examples.
This work is dedicated/deeded to the public following laws in jurisdictions
for such purpose.
The principal authors are:
You can always use the "Pages" UI control above this sidebar ☝ to navigate around all pages alphabetically (even pages not in this sidebar), to navigate the outline for each page, or for a search box.
Here is the suggested reading order: