Skip to main content

avoid-high-cyclomatic-complexity

added in: 1.23.0
⚙️
Pro+

Warns when the cyclomatic complexity of a method or function exceeds the configured threshold.

Cyclomatic complexity is the number of linearly independent paths through a block of code. Consider keeping the cyclomatic complexity low as the more paths you have, the higher the overall complexity and the number of test cases that needs to be implemented.

⚙️ Config

Set threshold (default is 20) to configure the complexity threshold.

analysis_options.yaml
dart_code_metrics:
rules:
- avoid-high-cyclomatic-complexity:
threshold: 20

Example

❌ Bad:

// LINT: Cyclomatic complexity for this declaration is N which exceeds the configured threshold. Try simplifying it.
void someComplexFunction() {
final d = c.isNotEmpty && true;

final e = c.isNotEmpty || false;

final f = c?.isNotEmpty;

final g = Object() ?? Object();

Object? h;
h ??= Object();
}

✅ Good:

void firstFn() {
final d = c.isNotEmpty && true;

final e = c.isNotEmpty || false;
}

void secondFn() {
final f = c?.isNotEmpty;

final g = Object() ?? Object();

Object? h;
h ??= Object();
}