All rules
Block tag punctuation¶
Description of certain block tags, if present, should end with a period, question mark or exclamation mark.
Before
After
Configuration
Checkstyle | Default value |
---|---|
BlockTagPunctuation#tags | @param, @return |
Ktlint | |
rulebook_punctuate_block_tags | @constructor, @receiver, @property, @param, @return |
Built-in types¶
Prefer to use built-in types provided by the language.
Before
After
Confusing predicate¶
Use the positive form in a predicate call when it is a single expression and the calling function can be inverted.
Before
After
File size¶
File length should not be longer than 1.000 lines of code. If a file exceeds the limit, it should be split into multiple files.
Before
After
Configuration
Checkstyle | Default value |
---|---|
FileLength#max | 1.000 |
CodeNarc | |
ClassSize#maxLines | 1.000 |
Ktlint | |
rulebook_max_file_size | 1.000 |
Pylint | |
rulebook-max-file-size | 1.000 |
Null equality¶
Use structural equality instead of referential equality when comparing objects
with null
.
Before
After
Redundant qualifier¶
Strip fully qualified names when they are already imported.
Before
After
TODO comment¶
TODO comment keywords should be uppercase and followed by exactly one space.
Before
After
Trailing comma in call¶
Put a trailing comma in a multiline call site, omit when it is a single line.
Before
After
Trailing comma in collection¶
Put a trailing comma in a multiline collection site, omit when it is a single line. In Java and Groovy, this rule applies to array initializers. In Python, this rule applies to tuples.
Before
After
Trailing comma in declaration¶
Put a trailing comma in a multiline declaration site, omit when it is a single line.
Before
After
Unused import¶
Remove unused import statements.
Before
After
Wildcard import¶
Import directives must be single-type instead of wildcard imports.
Before
After
Declaring¶
Abstract class definition¶
Abstract classes need at least one abstract function.
Before
After
Contract function definition¶
Kotlin contract functions that carry a runnable parameter should have inline
modifier. Without the modifier, user cannot assign a global variable within the
code block.
Before
After
Exception inheritance¶
Use Exception
as superclass of custom exceptions. Most applications should not
extend Error
or Throwable
.
Before
After
Number suffix for double¶
Double floating point literals should be suffixed with lowercase d
, which is
more readable than D
.
Before
After
Number suffix for float¶
Floating point literals should be suffixed with lowercase f
, which is more
readable than F
.
Before
After
Number suffix for integer¶
Integer literals should be suffixed with lowercase i
, which is more readable
than I
.
Before
After
Number suffix for long¶
Long integer literals should be suffixed with uppercase L
.
Before
After
String quotes¶
Before
After
Unnecessary parentheses in lambda¶
Single parameter lambdas should not have parentheses.
Before
After
Tip
Parentheses on lambda parameters is a syntax error in:
- Kotlin if parameter is single
- Groovy closures, not lambdas
Use single quotes for string literals, unless the string contains single quotes.
Utility class definition¶
Utility classes should have a final modifier and a private constructor to prevent instantiation.
Before
After
Naming¶
Class name acronym¶
Ensures that the first letter of acronyms longer than three characters are always capitalized.
Before
After
Class name¶
Class, interface and object names are written in PascalCase
.
Before
After
Constant property name¶
Constant fields should be written in SCREAMING_SNAKE_CASE
.
Before
After
File name¶
If the file contains a single class, the file name should be the same as the root class name.
Before
After
Identifier name¶
Non-constant fields, functions and parameters should be written in camelCase.
Before
After
Illegal class final name¶
Prohibits meaningless source names in class, interface, object and files. The name of utility classes (or files) should be the plural form of the extended class.
Before
After
Configuration
Checkstyle | Default value |
---|---|
IllegalClassFinalName#names | Util, Utility, Helper, Manager, Wrapper |
CodeNarc | |
IllegalClassFinalName#names | Util, Utility, Helper, Manager, Wrapper |
Ktlint | |
rulebook_illegal_class_final_names | Util, Utility, Helper, Manager, Wrapper |
Pylint | |
rulebook-illegal-class-final-names | Util, Utility, Helper, Manager, Wrapper |
Illegal variable name¶
Prohibitprimitive type names, base Object
type names and their plural forms as
identifiers of properties, parameters and local variables. The name of the
identifier should be descriptive and meaningful.
Before
After
Configuration
Checkstyle | Default value |
---|---|
IllegalIdentifierName#format | object, integer, string, objects, integers, strings |
CodeNarc | |
IllegalVariableName#names | object, integer, string, object, integers, strings |
Ktlint | |
rulebook_illegal_variable_names | any, boolean, byte, char, double, float, int, long, short, string, many, booleans, bytes, chars, doubles, floats, ints, longs, shorts |
Pylint | |
bad-names | objs, ints, strs |
Package name¶
Package names should be written in lowercase with no separators.
Before
After
Property name interop¶
Kotlin field definitions that are Boolean types should be prefixed with is
.
Otherwise, the compiler will generate a getter method with get
prefix.
Before
After
Required generic name¶
Only use common generic type names according to Oracle. Multiple generic types declaration is ignored.
Before
After
Configuration
Checkstyle | Default value |
---|---|
ClassTypeParameterName#format | E, K, N, T, V |
InterfaceTypeParameterName#format | E, K, N, T, V |
MethodTypeParameterName#format | E, K, N, T, V |
RecordTypeParameterName#format | E, K, N, T, V |
TypeParameterName#format | E, K, N, T, V |
CodeNarc | |
RequiredGenericName#names | E, K, N, T, V |
Ktlint | |
rulebook_required_generic_names | E, K, N, T, V |
Pylint | |
rulebook-required-generic-names | E, K, N, T, V |
Ordering¶
Block tag order¶
Block tags should be ordered in the following sequence: @constructor
,
@receiver
, @param
, @property
, @return
, @throws
, @see
.
Before
After
Built-in function position¶
Place Object built-in methods such as toString()
, hashCode()
and equals()
at the end of the class.
Before
After
Import order¶
Import directives should be ordered alphabetically without any blank lines.
Before
After
Inner class position¶
Place inner classes at the end of the class.
Before
After
Member order¶
The class should be organized as follows: properties, initializer block, constructors and methods.
Before
After
Overload function position¶
Place overloaded functions next to each other.
After
After
Static import position¶
Static import directives are to be placed after normal imports, separated by a blank line.
Before
After
Spacing¶
Block comment spaces¶
Ensures that block comments starts and ends with a whitespace. In multiline comments, each line after the asterisk should be indented by a whitespace.
Before
After
Block comment trim¶
Do not start or end block comments with whitespaces.
Before
After
Block tag indentation¶
Multi-line block tag descriptions should be indented by four spaces, or five spaces from the leading asterisk.
Before
After
Case separator¶
Multiline switch-case entries end with a blank line while short entries are joined.
Before
After
Code block trim¶
Prohibits empty first and last lines in code blocks.
Before
After
Comment spaces¶
End-of-file comments should be separated by a single whitespace from the preceding code, and start with a single whitespace.
Before
After
Warning
PEP8 requires leading two spaces for comments.
Comment trim¶
Prohibits empty first and last lines in EOL comments.
Before
After
Duplicate blank line¶
Prohibits consecutive blank lines in the code.
Before
Warning
PEP8 allows two blank lines between top-level functions and class definitions.
After
Duplicate blank line in block comment¶
Prohibits consecutive blank lines in block comments.
Before
After
Duplicate blank line in comment¶
Prohibits consecutive blank lines in comments.
Before
After
Member separator¶
Class, function and property declarations should be separated by a blank line. There is an exception for single-line properties.
Before
After
Missing blank line before block tags¶
Separate block tag group from the summary with a blank line.
Before
After
Unnecessary blank line before package¶
The first line of a file cannot be a blank line.
Before
After
Stating¶
Illegal catch¶
Catch specific exception subclass instead of the generic Throwable
,
Exception
or Error
.
Before
After
Illegal throw¶
Throw a narrower exception type instead of Exception
, Error
or Throwable
.
Before
After
Missing braces¶
Enforces the use of braces for multiline if
, else
, for
, while
and do
statements.
Before
After
Nested if-else¶
If a block ends with an if statement without else and the body is at least 2 lines, it should be inverted to avoid nesting and unnecessary indentation.
Before
After
Redundant default¶
If every branch of a switch statement has a return or throw statement, the default branch can be lifted.
Before
After
Redundant else¶
When every if and else-if block has a return or throw statement, the else block can be lifted.
Before
After
Unnecessary switch¶
If a switch statement has single branch, it should be replaced with an if statement.
Before
After
Wrapping¶
Assignment wrap¶
Assignee and the value of assignment spanning multiple lines should be separated by a newline.
Before
After
Chain call wrap¶
Each method call in a chain should be aligned with the dot operator.
Before
After
Elvis wrap¶
In a multiline statement, the elvis operator should be separated into a new line instead of trailing the statement.
Before
After
Empty code block join¶
Whitespace is not allowed in empty code blocks.
Before
After
Tip
Code blocks with comments are not considered empty.
Infix call wrap¶
When breaking an infix function call, the operator should be placed at the end of the line.
Before
After
Lambda wrap¶
When breaking a multiline lambda expression, the body should be placed on a new line.
Before
After
Operator wrap¶
A line break should be placed after the operator in a binary expression.
Before
After
Parameter wrap¶
When breaking a multiline parameter list, each parameter should be placed on a new line.
Before
After
Short block comment join¶
Short block comments should be written in a single line.
Before
After
Statement wrap¶
Compound statements are not allowed.
Before
After