cognitive-complexity
- Group
- 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
void handle(bool a, bool b) {
if (a) { while (b) { print(1); } }
}
void alternatives(int x) {
if (x == 1) {} else if (x == 2) {} else {}
}void handle(bool a, bool b) {}void alternatives(int x) {Valid
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.
{
"linter": {
"rules": {
"complexity": {
"cognitive-complexity": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/cognitive-complexity: <reason>More complexity rules
- cyclomatic-complexity
cyclomatic_complexityFlags a function whose cyclomatic complexity exceeds the configured threshold.
- join-return-with-assignment
join_return_with_assignmentFlags an assignment immediately followed by
returnof the same variable. - max-lines-for-file
max_lines_for_fileFlags a file longer than the configured line limit.
- max-lines-for-function
max_lines_for_functionFlags a function or method longer than the configured line limit.
- max-parameters-for-function
max_parameters_for_functionFlags callable declarations and function types with more than the configured number of parameters.
- max-statements
Reports function bodies with too many statements.
- max-switch-cases
max_switch_casesFlags a
switchstatement with more than the configured number of cases. - maximum-nesting-level
maximum_nesting_levelFlags a function whose control-flow nesting runs deeper than the configured level.