switch statement nested inside another switch
ID |
c.maintainability.nested_switch |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Control Flow |
Language |
C / C++ |
Description
This switch is nested inside another switch. Two independent dispatch tables in the same block are hard to read together, and a break inside the inner one only exits the inner switch — a common source of confusion with loop break. Extract the inner switch into its own function, or replace it with an if/else chain to make the two levels visually distinct.
Rationale
This switch is nested inside another switch. Two independent dispatch tables in the same block are hard to read together, and a break inside the inner one only exits the inner switch — a common source of confusion with loop break. Extract the inner switch into its own function, or replace it with an if/else chain to make the two levels visually distinct.
The following code illustrates the pattern detected by this rule:
void directNested(int x, int y) {
// FLAGGED: switch statement nested inside another switch
switch (x) {
case 1:
switch (y) {
case 1:
doA();
break;
default:
break;
}
break;
default:
break;
}
}