-consider-switch-as-single-decision
Compute cyclomatic complexity assuming a switch-case statement to be a single decision point
Syntax
-consider-switch-as-single-decision
Description
-consider-switch-as-single-decision changes how Polyspace® calculates the metric Cyclomatic Complexity and the Guideline Cyclomatic complexity
exceeds threshold. Cyclomatic complexity is the number of decision points
in your code plus one. By default, Polyspace counts each individual case of a switch-case statement as
a separate decision point if the case if terminated with a break
statement. When you specify this option, Polyspace counts the entire switch-case statement as a single
decision point.
Examples
Change in Value of Cyclomatic Complexity
In this example, the function factorial() selects a return value
using a switch-case statement.
int factorial(int in) {
int val = -1;
switch(in) {
case 1:
val = 1;
break;
case 2:
val = 2;
break;
case 3:
val = 6;
break;
default:
break;
}
return val;
}Cyclomatic Complexity as 4. When you specify this option, Polyspace considers the entire switch-case statement as a single
decision point, resulting in a cyclomatic complexity of 2.Change in Guideline Rule Violation
In this example, The cyclomatic complexity of factorial() is 8,
which is above the specified threshold 5, resulting in a violation of the guideline
Cyclomatic
complexity exceeds threshold.
int factorial(int in) { //Noncompliant
int val = -1;
switch(in) {
case 1:
val = 1;
break;
case 2:
val = 2;
break;
case 3:
val = 6;
break;
case 4:
val = 24;
break;
case 5:
val = 120;
break;
case 6:
val = 720;
break;
case 7:
val = 5040;
break;
default:
break;
}
return val;
}When you specify the option -consider-switch-as-single-decision, the cyclomatic complexity becomes
2 and the guideline Cyclomatic
complexity exceeds threshold is no longer violated.
Dependencies
This option is relevant when you calculate the code metrics or activate the Guidelines coding rules. See: