Collapsible If Statements
ID |
kotlin.collapsible_if |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
readability, simplification |
Description
Reports a nested if statement that can be collapsed into a single condition.
When an outer if has no else and its only body statement is an inner if that also
has no else, both guards can be merged with && into a single expression, reducing
nesting and improving readability.
Rationale
// Bad — unnecessary nesting
if (user != null) {
if (user.isActive) { // FLAW
sendEmail(user)
}
}
// Good — single combined condition
if (user != null && user.isActive) {
sendEmail(user)
}