Cyclomatic Complexity
Number of linearly independent paths in function body
Description
Note
Use Bug Finder instead of Code Prover for computing code metrics. Support for computing code metrics in Code Prover will be removed in a future release. See Version History.
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
if
statement is one decision point.The statements
for
andwhile
count as one decision point, even when no condition is evaluated, for example, in infinite loops.Boolean combinations
(&&, ||)
do not count as decision points.case
statements do not count as decision points unless they are followed by abreak
statement. 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-case
statement 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 |