cognitive-complexity

Path
lint/complexity/cognitive-complexity
Default
Off, opt in
Origin
Falcon
Rule
204 of 512

Reports function bodies with excessive nesting-weighted control flow.

max-complexity (integer, default: 15) limits the score. Ifs, loops, switches, ternaries, and catches cost one plus their nesting depth. Else-if and else cost one without extra nesting. Each connected logical operator sequence costs one, with another point when && changes to || or vice versa. Try/finally and plain blocks do not add complexity. Nested callable bodies are measured independently.

Invalid

example.dartdart
void handle(bool a, bool b) {
  if (a) { while (b) { print(1); } }
}
void alternatives(int x) {
  if (x == 1) {} else if (x == 2) {} else {}
}
Function has cognitive complexity 3 (max 2). Reduce nesting or extract helpers.
1void handle(bool a, bool b) {
Function has cognitive complexity 3 (max 2). Reduce nesting or extract helpers.
3}
4void alternatives(int x) {

Valid

example.dartdart
void handle(bool a, bool b) { if (a) { print(1); } if (b) { print(2); } }
void outer(bool a) { void inner() { if (a) {} } if (a) {} }
void plain() { try { print(1); } finally { print(2); } }

Configure cognitive-complexity

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "cognitive-complexity": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/cognitive-complexity: <reason>
  • cyclomatic-complexitycyclomatic_complexity

    Flags a function whose cyclomatic complexity exceeds the configured threshold.

  • join-return-with-assignmentjoin_return_with_assignment

    Flags an assignment immediately followed by return of the same variable.

  • max-lines-for-filemax_lines_for_file

    Flags a file longer than the configured line limit.

  • max-lines-for-functionmax_lines_for_function

    Flags a function or method longer than the configured line limit.

  • max-parameters-for-functionmax_parameters_for_function

    Flags callable declarations and function types with more than the configured number of parameters.

  • max-statements

    Reports function bodies with too many statements.

  • max-switch-casesmax_switch_cases

    Flags a switch statement with more than the configured number of cases.

  • maximum-nesting-levelmaximum_nesting_level

    Flags a function whose control-flow nesting runs deeper than the configured level.