Deeply nested if statements (three or more levels)

ID

c.maintainability.nested_if

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Complexity

Language

C / C++

Description

This if is nested three levels deep (fixed threshold: 3). Deeply nested conditionals are hard to follow and test. Reduce nesting with early returns / guard clauses, by combining conditions, or by extracting the inner logic into a helper function.

Rationale

This if is nested three levels deep (fixed threshold: 3). Deeply nested conditionals are hard to follow and test. Reduce nesting with early returns / guard clauses, by combining conditions, or by extracting the inner logic into a helper function.

The following code illustrates the pattern detected by this rule:

int deep(int a, int b, int c, int x) {
    // FLAGGED: Deeply nested if statements (three or more levels)
    if (a > 0) {
        if (b > 0) {
            if (c > 0) {
                return x;
            }
        }
    }
    return 0;
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.