KT.COMPLEX_CONDITION

Complex conditions should be extracted into into well-named functions or variables

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.

Noncompliant Code

Copy
val str = "foo"
val isFoo = if (str.startsWith("foo") && !str.endsWith("foo") && !str.endsWith("bar") && !str.endsWith("_")) {
  // ...
}

Compliant Code

Copy
val str = "foo"
val isFoo = if (str.startsWith("foo") && hasCorrectEnding()) {
  // ...
}

Options

  • threshold (default: 4)

    the number of conditions which will trigger the rule

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html