Common rationales
Abstract class require abstract method¶
Abstract modifier in a class is unnecessary if the class has no abstract methods.
Avoid primitive names¶
When declaring string and primitive variables, it is tempting to use simple names based on their type. In fact, they are usually recommendations given by the IDE. But these names are not descriptive and hard to understand what their purpose is.
Concise brackets, parentheses and tags¶
Brackets and parentheses without content should be wrapped in a single line.
Delete empty file¶
Content of a file should not be empty or consist of only whitespace characters, which may be spaces, tabs or newlines.
Hide utility class instance¶
To prevent instantiating a utility class, put a final modifier on the class and add a private constructor.
class Strings {
public Strings() {}
static String capitalize(String str) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}
Move trailing elvis to the next line¶
In a multiline statement, the elvis operator should align with chained method calls instead of the same column.
No blank lines at initial, final and consecutive comments¶
Comment and block comment bodies should not have leading or trailing blank lines. No consecutive blank lines are allowed in the comment body.
/**
*
* The main function.
*
*/
fun main() {
// initialize the logger
//
//
// and print the starting message
val logger = Logger()
logger.info("Starting the application")
}
Remove negation¶
In functions that support both positive and negative conditions, the positive condition should be preferred.
Sort named imports¶
When importing multiple members from the same package, they should be sorted alphabetically.
Trim multiline brackets, parentheses and tags¶
Method declaration, calls and collection initializers should not start or end with a blank line.
Use else-if¶
Single if statement in an else block should be converted to an else-if statement.
if (x > 0) {
println("Positive")
} else {
if (x < 0) {
println("Negative")
} else {
println("Zero")
}
}
Use named object directly¶
Single calls in Gradle NamedDomainObjectContainer should not be wrapped in a
block.