Complex Condition

ID

kotlin.complex_condition

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

complexity, readability

Description

Reports if and loop conditions that contain three or more boolean operators (&&, ||, !). The threshold is configurable via the threshold property (default: 3).

Rationale

A condition packed with boolean operators is hard to read, reason about, and test. Extracting sub-expressions into well-named variables makes the intent explicit and reduces the chance of operator-precedence bugs.

// Bad — 4 boolean operators
fun check(a: Boolean, b: Boolean, c: Boolean, d: Boolean): Boolean {
    if (a && b || c && !d) { // FLAW
        return true
    }
    return false
}

// Good
fun check(a: Boolean, b: Boolean, c: Boolean, d: Boolean): Boolean {
    val firstGroup = a && b
    val secondGroup = c && !d
    if (firstGroup || secondGroup) {
        return true
    }
    return false
}

Remediation

Extract compound sub-expressions into named val variables that describe their purpose, then combine the variables in the condition.