Skip to content

max_cyclomatic_complexity

Reports a function whose cyclomatic complexity is over the limit. Split its decisions into smaller functions.

Tier
advisory
Scope
Dart
Fix
none

A function that makes more decisions than the limit max allows. A function exactly at max is not reported. The violation is on the function’s name, and its message names the value and the limit:

This function has a cyclomatic complexity of 19, which is over the limit of 15.

  • How it is counted: a function starts at 1, and each if, loop, case, catch, conditional expression, &&, || and ?? adds 1. An else, a default and a finally add nothing. See How the complexity is counted.
  • What is measured: every top-level function, method, getter, setter, operator and constructor with a body, factory constructors included. A local function or a closure counts its own decisions, not those of the function around it. A closure’s violation is on its parameter list: (item) in items.where((item) { … }).
  • One violation, not two: where max_cognitive_complexity reports the same function, only that rule reports it, and its message gives this rule’s value.

Each decision is another case you have to keep apart when you read a function, and another path that needs a test: a function of complexity 20 has at least twenty ways through it, and almost never twenty tests. Code written by an AI coding agent gathers decisions in one place, one more if or && at a time, and a limit counts them. The rule is advisory because the number is a signal, not a proof: a flat switch over twenty values is high and plain, and a short function with three interlocked conditions is low and hard.

Violatingwith max: 6
double shippingCost(Order order) {
if (order.items.isEmpty) return 0;
var cost = 4.0;
if (order.weight > 20 || order.isBulky) {
cost += 12;
} else if (order.weight > 5) {
cost += 5;
}
if (order.country != 'DE' && order.country != 'AT') {
cost += order.isInEurope ? 8 : 25;
}
if (order.isExpress) cost *= 2;
return order.customer.isMember ? cost * 0.8 : cost;
}
CorrectedIts complexity is 10.
double shippingCost(Order order) {
if (order.items.isEmpty) return 0;
var cost = 4.0 + weightSurcharge(order) + distanceSurcharge(order);
if (order.isExpress) cost *= 2;
return order.customer.isMember ? cost * 0.8 : cost;
}
double weightSurcharge(Order order) {
if (order.weight > 20 || order.isBulky) return 12;
if (order.weight > 5) return 5;
return 0;
}
double distanceSurcharge(Order order) {
if (order.country == 'DE' || order.country == 'AT') return 0;
return order.isInEurope ? 8 : 25;
}

Each function now makes one kind of decision, and none is over 4.

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
max none in the rule; the presets give it The highest cyclomatic complexity a function may have. A whole number, zero or more.
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

The rule has no limit of its own. The presets give one:

Preset max
core the rule is not in this preset
recommended 15
strict 10

To change it:

assay:
rules:
max_cyclomatic_complexity:
max: 20

Write why on the line directly above the suppression. It goes above the function’s name, where the violation is, or at the end of that line:

// The cases follow the table of the tax authority, one for one.
// ignore: assay/max_cyclomatic_complexity
double taxRate(String category, String region) {

Where max_cognitive_complexity reports the function, a suppression of this rule hides nothing, since that rule carries the violation. Suppress that rule instead: it covers this one too.

// ignore_for_file: assay/max_cyclomatic_complexity silences it in a whole file, and // ignore: max_cyclomatic_complexity without the prefix works too. See Suppressions.

All cases

A function starts at 1, and each branch point in it adds 1:

Branch point Counts
if one for each if, also an else if, an if with a case, and an if inside a collection literal
loop one for each for, for-in, await for, while and do, and for a for inside a collection literal
case one for each case of a switch statement and for each case of a switch expression
catch one for each catch clause of a try, with or without on
conditional expression one for each condition ? a : b
&&, ||, ?? one for each operator

An else, a default, a finally and a case that is only the wildcard _ add nothing: they are the path left when every decision is made. A constructor’s initializers count with its body.

  • Every case counts, also where several share one body: case 1: case 2: is two. A switch that covers every value of an enum without a default counts one more than the same switch with a default for the last value.
  • Only the bare wildcard _ is taken for a default. A case that also matches everything, such as var other or Object _, counts.
  • Inside a pattern, || and && are not counted: case 1 || 2: is one case. The && and || of a guard are counted.
  • ??=, ?., ?[, ...?, ! and a null-aware element ?value are not counted.
  • assert is not a branch point, but the && and || in its condition are counted.
  • A parameter’s default value is not part of the body and is not counted.

So this function has complexity 5: 1, the loop, the if, the &&, the conditional expression.

int total(List<int> prices, {required bool capped}) {
var sum = 0;
for (final price in prices) {
if (price > 0 && price < 1000) {
sum += price;
}
}
return capped ? sum.clamp(0, 5000) : sum;
}

A function with no branch point has complexity 1, and so has a declaration with no body.

A function declared inside another one counts its own branch points, not the function around it: a function that holds three closures of complexity 4 each, and decides nothing itself, has complexity 1.

This rule reports as usual where that rule does not judge the file (the preset does not switch it on, enabled: false, its exclude covers the file, or no limit is in effect) and at a function under that rule’s limit. Both hosts decide this the same way, before any suppression.

Where no limit is in effect, the rule reports nothing. Every function has a complexity of at least 1, so max: 0 reports every function.