Conditional blocks nested three or more levels deep

ID

dart.maintainability.deep_nesting

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Complexity

Language

Dart

Description

Reports an if that contains an if that contains a third if. The threshold is fixed at three levels, and deeper nests match too because the innermost level is reached through the same containment. An else if chain is not nesting and is not reported.

Rationale

Every level of nesting multiplies the paths a reader has to hold in mind, and the condition that actually guards the innermost statement is the conjunction of all three - which is nowhere written down. Deeply nested code is correspondingly hard to test, since covering the inner branch means arranging every outer condition first, and it is where off-by-one guards and misplaced else branches hide.

The following code illustrates the pattern detected by this rule:

class Router {
  void dispatch(bool a, bool b, bool c) {
    // FLAGGED: Conditional blocks nested three or more levels deep
    if (a) {
      log('one');
      if (b) {
        log('two');
        if (c) {
          log('three');
        }
      }
    }
  }

  void log(String m) {}
}

Remediation

Invert the outer conditions into guard clauses that return or continue early, so the main path stays at the left margin. Where the conditions are genuinely independent, combine them with && into a single test, or extract the inner branches into named methods whose names explain what each case is for.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved