-
Notifications
You must be signed in to change notification settings - Fork 5
Java coding guidelines
adidas Java coding guidelines are based on Google Java Style Guide with minor customization.
A source file must always have the following structure (in given order), with one empty line between them:
* Copyright notice
* Package definition
* Import statements (zero or more)
* Class API documentation comment
* Class code
- Variables (static, then object, also includes enums)
- Constructors
- Methods
- Functional methods
- Getters / setters / delegates
- Inner classes
- Do not use tabs, use plus 2 spaces.
- Continuation lines use plus 4 spaces.
- Separating any reserved word
if
,else
,for
orcatch
from any open/close parenthesis(
/)
or brace{
/}
. - Around of any binary or ternary operator.
- Around colon
:
in afor
statement. - Around arrow
->
in a lambda expression.
We follow the K&R variant: 1TBS.
/* Correct use */
if (y > 1) {
increase(y);
} else {
decrease(y);
}
/* Incorrect use */
if (y > 1)
{
increase(y);
}
else
{
decrease(y);
}
/* Incorrect use */
if (y > 1) increase(y);
else decrease(y);
/* Correct use */
void doNothing() { }
/* Incorrect use */
void doNothing() {
}
We have to follow the limit of 100 character per line taking into account the next exceptions:
- Package and import statements.
/* Correct use */
List<Client> clients = new ArrayList<Client>()
.add(new Client("John"))
.add(new Client ("Jean"))
.add(new Client ("Mark"));
/* Incorrect use */
List<Client> clients = new ArrayList<Client> ().add(new Client("John"))
.add(new Client ("Jean")).add(new Client ("Mark"));
Naming rule | Format |
---|---|
Package | lowercase, in case a package contains more than one word, just the concatenation of them in lowercase, without underscore. |
Constants | UPPER_SNAKE |
Variables | lowerCamelCase |
Class | UpperCamelCase |
It covers only comments in the code , there is a specific section related with Javadoc.
Find below examples about the different ways accepted to comment the code.
/*
* This is a multiline comment.
*/
/* Inline comment */
// Inline comment.
Javadoc is mandatory for every public class and every public or protected method part of this class. The general formatting of Javadoc block is the detailed below.
/**
* Writting here multiple lines of Javadoc text.
*
* <p>Example of a second paragraph.</p>
*
* @param key The value key.
* @param value The value sample.
* @return The absolute value.
* @throws CustomException if the value does not exist.
*/
Each paragraphs contained in a Javadoc text have to start with <p>
just before the first word.
The use of any of the standard block tags should be done in the next order @param
as many occurrences as parameters in order, @return
and @throws
. After them, it is mandatory to add a description.
- Code must not contain empty
catch
blocks, only in some special cases is allowed to keep acatch
block empty and it must be documented properly justifying it. - A proper exception hierarchy must be present.
Mark a method with the @override
annotation whenever you are overriding a method.
- It will increase the readability of the code.
- The compiler will check that method in the parent class. It can help the developers to avoid mistakes such as wrong parameter types or method name.