Pattern: Complex condition
Issue: -
Complex conditions make it hard to understand which cases lead to the condition being true
or false
. To improve
readability and understanding of complex conditions consider extracting them into well-named functions or variables
and call those instead.
Example of incorrect code:
val str = "foo"
val isFoo = if (str.startsWith("foo") && !str.endsWith("foo") && !str.endsWith("bar") && !str.endsWith("_")) {
// ...
}
Example of correct code:
val str = "foo"
val isFoo = if (str.startsWith("foo") && hasCorrectEnding()) {
// ...
}
fun hasCorrectEnding() = return !str.endsWith("foo") && !str.endsWith("bar") && !str.endsWith("_")