KT.COMPLEX_METHOD

Complex method with high Cyclomatic Complexity

Complex methods are hard to understand and read. It might not be obvious what side-effects a complex method has. Prefer splitting up complex methods into smaller methods that are in turn easier to understand. Smaller methods can also be named much clearer which leads to improved readability of the code. This rule uses McCabe's Cyclomatic Complexity (MCC) metric to measure the number of linearly independent paths through a function's source code (https://www.ndepend.com/docs/code-metrics#CC). The higher the number of independent paths, the more complex a method is. Complex methods use too many of the following statements. Each one of them adds one to the complexity count. Conditional statements - if, else if, when Jump statements - continue, break Loops - for, while, do-while, forEach Operators &&, ||, ?: Exceptions - catch, use Scope Functions - let, run, with, apply

Options

  • threshold (default: 15)

    McCabe's Cyclomatic Complexity (MCC) number for a method.

  • ignoreSingleWhenExpression (default: False)

    Ignores a complex method if it only contains a single when expression.

  • ignoreSimpleWhenEntries (default: False)

    Whether to ignore simple (braceless) when entries.

  • ignoreNestingFunctions (default: False)

    Whether to ignore functions which are often used instead of an if or for statement.

  • nestingFunctions (default: ['also', 'apply', 'foreach', 'isnotnull', 'ifnull', 'let', 'run', 'use', 'with'])

    Comma separated list of function names which add complexity.

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