Keep things minimal

Minimal code refers to expressing things in shorter rather than longer terms (without changing the underlying logic).
This can severely reduce the time that developers have to spend visually parsing the code you wrote.

"Without changing the underlying logic" is the important part here, as you can easily vary the LOC you write depending on what code you write, obviously.

This mostly revolves around using the tools that the programming language (and your IDE) provides you with, as writing idiomatic code can help you reduce visual complexity by half very often.

General examples

// Bad: Overcomplicated If-Else
fun isAdult(age: Int): Boolean {
    if (age >= 18) {
        return true
    } else {
        return false
    }
}
// Good: Use a Simple Expression -> 1 line to parse instead of 6
fun isAdult(age: Int) = age >= 18

Use Idiomatic Kotlin

// Bad: non-idiomatic
val names = listOf("Alice", "Bob", "Charlie")
for (i in 0 until names.size) {
    println(names[i])
}
// Good: idiomatic
names.forEach { println(it) }

// Bad: not minimal
fun getColorName(color: String): String {
    val name = when (color) {
        "red" -> "Red"
        "blue" -> "Blue"
        else -> "Unknown"
    }
    return name
}
// Good: minimal
fun getColorName(color: String) = when (color) {
    "red" -> "Red"
    "blue" -> "Blue"
    else -> "Unknown"
}

Slightly overdone example

// Bad: Not minimal, can easily cause potential bugs
fun isAffectedUser(user: User?): Boolean {
    return if (user != null) {
        if (user.isActive) {
            if (user.age > 18) {
                true
            } else {
                false
            }
        } else {
            false
        }
    } else {
        false
    }
}
// Good: minimal (causes simple and immediately clear logic, good visual parsability)
fun isAffectedUser(user: User?) = user?.isActive == true && user.age > 18

Examples


Make an active effort to keep visual processing time down.

The difference between these two examples is tiny, but which of these two is faster to visually process?

private fun getSignature(): Signature {
    val sig = when (keyType) {
        KeyType.secp256k1 -> Signature.getInstance(
            "SHA256withECDSA",
            "BC"
        )
        KeyType.secp256r1 -> Signature.getInstance("SHA256withECDSA")
        KeyType.Ed25519 -> Signature.getInstance("Ed25519")
        KeyType.RSA -> Signature.getInstance("SHA256withRSA")
    }
    return sig
}
private fun getSignature(): Signature =
    when (keyType) {
        KeyType.secp256k1 -> Signature.getInstance("SHA256withECDSA", "BC")
        KeyType.secp256r1 -> Signature.getInstance("SHA256withECDSA")
        KeyType.Ed25519 -> Signature.getInstance("Ed25519")
        KeyType.RSA -> Signature.getInstance("SHA256withRSA")
    }