A function is harder to follow than the configured maximum, counting branches by how deeply they are nested

tx0/kotlin:nesting-weighted-complexityrecommendedwarningmaintainability/complexitydepth fastmaintainability

What it finds

Cyclomatic complexity counts paths; this measure counts how hard the code is to read. Each if, when, loop and catch adds 1, plus 1 for every structure, lambda or local function it is nested in; an else if adds 1 without the nesting. Each run of the same && or || in a condition adds 1, and so does a jump to a label. The maximum is the parameter max, 15 by default.

From the rule's source, cognitive_complexity.rs.

Example from the rule's tests
fun tangled(items: List<Int>, flag: Boolean, other: Boolean): Int {tx0 reports: tangled has a cognitive complexity of 18, more than 15    var total = 0    for (item in items) {        if (item > 0) {            if (flag && other || item > 10) {                when (item) {                    1 -> total++                    else -> {                        while (total < item) {                            total += 1                        }                    }                }            } else if (other) {                total--            }        }    }    return total} fun flat(a: Int): Int {    if (a > 0) return 1    if (a < 0) return -1    return 0}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:nesting-weighted-complexity" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:nesting-weighted-complexity] reason="..."

tx0 explain tx0/kotlin:nesting-weighted-complexity shows the same in the terminal. Found a false positive? Report it.