Cyclomatic Complexity
Number of linearly independent paths in function body
Description
This metric calculates the number of decision points in a function and adds one to the total. A decision point is a statement that causes your program to branch into two paths.
The recommended upper limit for this metric is 10. If the cyclomatic complexity is high, the code is both difficult to read and can cause more orange checks. Therefore, try to limit the value of this metric.
To enforce limits on metrics, see Compute Code Complexity Metrics Using Polyspace.
Computation Details
The metric calculation uses the following rules to identify decision points:
An
ifstatement is one decision point.The statements
forandwhilecount as one decision point, even when no condition is evaluated, for example, in infinite loops.Boolean combinations
(&&, ||)do not count as decision points.casestatements do not count as decision points unless they are followed by abreakstatement. For instance, this code has a cyclomatic complexity of three:switch(num) { case 0: case 1: break; case 2: break; case 3: case 4: }If you specify the option
-consider-switch-as-single-decision, Polyspace® considers the entireswitch-casestatement as a single decision point and the cyclomatic complexity of the preceding code becomes two.The calculation is done after preprocessing:
Macros are expanded.
Conditional compilation is applied. The blocks hidden by preprocessing directives are ignored.
Examples
Metric Information
| Group: Function |
Acronym: VG |
| HIS Metric: Yes |