Nested if whose outer branch holds nothing else
ID |
scala.maintainability.collapsible_if |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Complexity |
Language |
Scala |
Description
Reports an if whose body contains only another if, with neither of them carrying an else branch. The two conditions can be merged into one.
Rationale
The outer if adds a level of indentation and a second decision point without expressing a second decision - both conditions have to hold for anything to happen. Flattening removes a branch from the code’s cyclomatic complexity, shortens the path a reader has to follow, and makes it obvious that the two tests belong together.
The following code illustrates the pattern detected by this rule:
class CollapsibleIf {
def bothSet(a: Boolean, b: Boolean): Unit = {
// FLAGGED: Nested if whose outer branch holds nothing else
if (a) {
if (b) {
println("both")
}
}
}
}