Cyclomatic Complexity
Description
Cyclomatic complexity measures the number of linearly independent paths in a function body. A high cyclomatic complexity indicates that the function has many linearly independent paths and requires a high number of test cases to evaluate the paths. To reduce the cyclomatic complexity of your code, consider refactoring the complex functions.
Polyspace Implementation
Polyspace® evaluates the cyclomatic complexity of a function as:
Where:
N is the number of conditions in the code.
on is the number of outcomes for the nth decision point.
Polyspace adds 1 to the number of linearly independent paths for each function. For instance, consider this function:
void evalNum(int x)
{
if (x > 0) // decision #1
printf( "x is positive" );
else if (x < 0) // decision #2
printf( "x is negative" );
else
printf( "x is 0" );
}N = 2
o1 = 2
o2 = 2
Number of function = 1
Then, the cyclomatic complexity is c = (2-1)
+ (2-1) + 1 = 3, indicating that the code snippet
has three linearly independent paths.
Examples
Version History
Introduced in R2023b