KT.NESTED_SCOPE_FUNCTIONS

Nested scope functions

Although the scope functions are a way of making the code more concise, avoid overusing them: it can decrease your code readability and lead to errors. Avoid nesting scope functions and be careful when chaining them: it's easy to get confused about the current context object and the value of this or it.

Noncompliant Code

Copy
// Try to figure out, what changed, without knowing the details
first.apply {
    second.apply {
        b = a
        c = b
    }
}

Compliant Code

Copy
// 'a' is a property of current class
// 'b' is a property of class 'first'
// 'c' is a property of class 'second'
first.b = this.a
second.c = first.b

Options

  • threshold (default: 1)

    Number of nested scope functions allowed.

  • functions (default: ['kotlin.apply', 'kotlin.run', 'kotlin.with', 'kotlin.let', 'kotlin.also'])

    Set of scope function names which add complexity. Function names have to be fully qualified. For example 'kotlin.apply'.

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