Main Content

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 and while 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 a break 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 entire switch-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

expand all

int foo(int x,int y)
{
    int flag;
    if (x <= 0)
        /* Decision point 1*/
        flag = 1;
    else
    {
        if (x < y )
            /* Decision point 2*/
            flag = 1;
        else if (x==y)
            /* Decision point 3*/
            flag = 0;
        else
            flag = -1;
    }
    return flag;
}

In this example, the cyclomatic complexity of foo is 4.

int foo (int x, int y) {
    if((x <0) ||(y < 0))
        /* Decision point 1*/
        return 0;
    else
        return (x > y ? x: y);
        /* Decision point 2*/
}

In this example, the cyclomatic complexity of foo is 3. The ? operator is the second decision point.

#include <stdio.h>


int foo(int x,int y, int ch)
{
    int val = 0;
    switch(ch) {
    case 1:
        /* Decision point 1*/
        val = x + y;
        break;
    case 2:
        /* Decision point 2*/
        val = x - y;
        break;
    default:
        printf("Invalid choice.");
    }
    return val;
}

In this example, the cyclomatic complexity of foo is 3.

int foo(int x,int y, int bound)
{
    int count = 0;
    if (x <= y)
        /* Decision point 1*/
        count = 1;
    else
        while(x>y) {
            /* Decision point 2*/
            x--;
            if(count< bound) {
                /* Decision point 3*/
                count++;
            }
        }
    return count;
}

In this example, the cyclomatic complexity of foo is 4.

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;
}
By default, Polyspace reports the value of 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.

Metric Information

Group: Function
Acronym: VG
HIS Metric: Yes

Version History

expand all