-
Notifications
You must be signed in to change notification settings - Fork 5
Coding style
Herman De Beukelaer edited this page Jun 3, 2016
·
6 revisions
This document describes the coding standards followed in the CoreHunter 3 project. Coding style is automatically verified with the Maven Checkstyle plugin during the verify phase. Any violations will fail the build. The checkstyle configuration file is contained in the CoreHunter build-tools module.
- Each top-level class, interface of enum resides in a source file of its own.
- The outer type name and file name should match. For example, a class
Foois required to be defined in a fileFoo.java.
- Star imports are not allowed.
- Wrapping import statements is not allowed.
- Package names are all lowercase, with consecutive words simply concatenated together (no underscores). Digits are not allowed before the first dot nor immediately after a dot. Regex:
^[a-z]+(\.[a-z][a-z0-9]*)*$. - Wrapping package statements is not allowed.
- Tab characters are not allowed. Four spaces are used for indentation.
- Comments are indented at the same level as the surrounding code.
- The maximum line length is 120 characters, except for package or import statements and lines containing an URL. Wrapping package or import statements is not allowed.
- Whitespace around the Generic tokens (angle brackets)
<and>should correspond to the typical convention. For more details, see http://checkstyle.sourceforge.net/config_whitespace.html#GenericWhitespace. - Multiple statements per line are not allowed.
- Long-valued integer literals use an uppercase
Lsuffix, never lowercase (to avoid confusion with the digit1). - Empty line separators are required after header, package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers. Static imports should be separated from other imports.
- When a line is broken at a dot, the break comes before the dot.
- When a line is broken at a comma, the break comes after the comma.
- When a line is broken at an operator, the break comes before the symbol.
- All names of classes, variables, methods, etc. consist of letters and digits only and are formatted in camel case, except for constants (see below). The abbreviation
ITis allowed in the name of integration test classes. AlsoAPIis allowed. - The names of classes, interfaces, enums and annotations start with an upper case letter.
- The names of non-static fields, static non-final fields, method and catch parameters, local variables and methods start with a lower case letter.
- The names of constants (static final fields) match the pattern
^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$. This means that the names must contain only upper case letters and digits. Words are separated with underscores and the name is required to start with a letter. Multiple consecutive underscores are not allowed. - For the name of type variables used in generic classes, interfaces and methods, there are two allowed styles:
- A single capital letter, optionally followed by a single number (e.g.
E,T,T2). - A name that complies with the rules for classes (see above) followed by a capital letter
Tor the wordType. - Overload methods are grouped together.
- For a method definition, constructor definition, method call or constructor invocation, the left parenthesis is required to come immediately after the identifier, i.e. on the same line and without whitespace after the identifier.
- Braces are compulsory, also in case of single-line blocks.
- The opening brace is on the end of the line. Only for enums this rule can be broken.
- The closing brace is preceded with a line break. However, an empty block or block-like construct may be closed immediately after it is opened (
{}), unless it is part of a multi-block statement (one that directly contains multiple blocks:if/else-if/elseortry/catch/finally). A line break is added after the closing brace if that brace terminates a statement or the body of a method, constructor or named class. For example, there is no line break after the brace if it is followed byelseor a comma. - A
switchblock always includes adefaultcase, even if it contains no code.
- The square brackets form a part of the type, not the variable: e.g.
String[] args, notString args[].
- It is not allowed to define
finalize()methods in a class.
- Empty
if,else,try,catch,finallyandswitchblocks should contain a comment. - A fall-through in a
switchstatement should be commented.
- The following at-clauses appear in this order if present:
@param,@return,@throws,@deprecated. - An at-clause is always followed by a description.
- When line-wrapping the description of an at-clause, an indentation of at least four spaces is used.