Deeply nested loops (three or more levels)
ID |
c.maintainability.deep_nesting |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Complexity |
Language |
C / C++ |
Description
This loop is nested three levels deep inside other loops (fixed threshold: 3). Deep nesting is hard to read and reason about, and often hides quadratic/cubic complexity. Extract the inner loops into well-named helper functions or rethink the algorithm to flatten the structure.
Rationale
This loop is nested three levels deep inside other loops (fixed threshold: 3). Deep nesting is hard to read and reason about, and often hides quadratic/cubic complexity. Extract the inner loops into well-named helper functions or rethink the algorithm to flatten the structure.
The following code illustrates the pattern detected by this rule:
void triple(int n, int m, int k, int grid[]) {
// FLAGGED: Deeply nested loops (three or more levels)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int l = 0; l < k; l++) {
grid[i] += j * l;
}
}
}
}