Skip to content

Nesting depth

The nesting depth of a function is the largest number of its control-flow statements that stand one inside the other. A statement five levels deep runs only when five conditions hold at once, and you have to carry all five to know when.

It is one of the five metrics of a function. A local function or a closure has a value of its own, counted from depth 0, and adds no level to the function around it.

The largest number of control-flow statements (if, for, for-in, while, do, switch, try) that stand one inside the other.

  • The body itself is depth 0.
  • An else and an else if are at the depth of their if. The cases of a switch, and the catch and finally of a try, are one level inside it.
  • A bare block and an expression add no level.

The page of the limit rule has every case: How the depth is counted.

Future<void> upload(List<Upload> uploads) async {
for (final upload in uploads) {
if (upload.isReady) {
try {
await send(upload);
} on TimeoutException {
if (upload.canRetry) {
await send(upload);
}
}
} else {
upload.skip();
}
}
}

Its nesting depth is 4:

Statement Depth
the for 1
if (upload.isReady) 2
the try 3
if (upload.canRetry), in the catch clause, which is one level inside its try 4
the else, which is at the depth of its if 2

The limit rule is max_nesting_depth, advisory: a function whose value is over the limit max is a violation of it. A function exactly at max is not reported.

Preset Limit
core none: the preset does not hold the rule
recommended 4
strict 3

The example is at the limit of recommended, so it is not reported there, and over the limit of strict.

To change the limit, set the rule’s max (Configuring metrics):

assay:
rules:
max_nesting_depth:
max: 5

Where max_cognitive_complexity reports the same function, it takes this violation over, and the report marks the value taken over (Over the limit, suppressed, taken over).

assay metrics gives the metric a row in the box Metrics and a box of its own, with the limit and the counts on the right of its top line and the highest values first. Under strict, with a function over the cognitive and the nesting limits:

╭─ Nesting depth ──────────────────────────── limit 3 · 2 over · 16 functions ─╮
│ Value File Line Name │
│ ───────────────────────────────────────────────────────────── │
│ ! 4 lib/over_limits.dart 13:5 deeplyNested │
│ ! 4 lib/over_limits.dart 99:5 nestedCount taken over │
│ 2 lib/measured.dart 11:7 Cart.total │
│ 1 lib/over_limits.dart 27:8 nameOf │
│ 0 lib/measured.dart 3:3 Cart │
│ 0 lib/measured.dart 9:11 Cart.count │
│ 0 lib/measured.dart 21:15 Cart.empty │
│ 0 lib/measured.dart 26:11 Priced.price │
│ 0 lib/measured.dart 28:8 Priced.isFree │
│ 0 lib/measured.dart 32:5 largest │
│ 6 more values, listed by `assay metrics --reporter json` │
╰──────────────────────────────────────────────────────────────────────────────╯
  • JSON: the key nestingDepth of each function, with its value, its limit and whether it is over it. A value taken over names the rule that takes it over in takenOverBy.
  • HTML: the column Nesting of the table of functions.
  • Trend: the series Nesting depth over limit (Points and series).

Every form is on assay metrics.