rulebook

Personal lint rules and code convention

View the Project on GitHub hendraanggrian/ruleboo

Rules

Rules Checkstyle KtLint
All name acronym AbbreviationAsWordInName
Class body starting whitespace
Functions return type
Throw exception ambiguity
Types Kotlin API

Documentation Rules

Rules Checkstyle KtLint
Summary continuation
Tag description sentence
Tags starting whitespace RequireEmptyLineBeforeBlockTagGroup

All name acronym

While uppercase acronym does comply with pascal-case naming standards, lowercase acronym is easier to read. However, only 3 connecting uppercase letters are flagged. This rule affects property, function, class-alike and even file.

val userJSON = "{ user: \"Hendra Anggrian\" }"
fun blendARGB()
class RestAPI
SQLUtility.kt

Class body starting whitespace

If a class' declaration is multiline and has a content, the first line of the content should be an empty line. This rule affects all class-like objects (class, interface, etc.).

class User(
    username: String,
    password: String
) {
    override fun toString(): String = username
}

Functions return type

Prohibits declaration of public expression function and property accessor without return type.

fun getMessage() = "Hello World"
val message get() = "Hello World"

Throw exception ambiguity

Throwing Exception, Error, and Throwable require a detail message. Does not apply to their subtypes (IllegalStateExeption, StackOverflowError, etc.) as they are explicit enough.

throw Exception()

Types Kotlin API

Avoid using API from java.* and org.junit.* where there are Kotlin equivalents.

import java.lang.String
val images = java.util.ArrayList<Image>()

Summary continuation

Every new line of continuation paragraph cannot start with link or code.

/**
 * This is a superclass of all
 * [cars] and
 * `bikes`.
 */
interface Vehicle

This is a default behavior of markdown editor on IntelliJ IDEs.

Tag description sentence

Description of certain tags, if present, is in form of a sentence. Therefore, must have one of end punctuations ., !, or ?.

/**
 * @param message a simple text
 */
fun send(message: String)

Tags starting whitespace

Both Javadoc and Kdoc recommend putting an empty space separating summary and tag group.

/**
 * Insert an item to this collection.
 * @param element item to add.
 * @return true if element is successfully added.
 */
fun add(element: E): Boolean