Main Content

Number of static paths exceeds threshold

The number of static paths in a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of static paths in the function is greater than the defined checker threshold. For details about how Polyspace calculates the number of static paths in a function, see Number of Paths

Polyspace® uses the default threshold 80 unless you specify a threshold. To specify a selection file where you can set the threshold, use the option Set checkers by file (-checkers-selection-file) or Checkers activation file (-checkers-activation-file).

When you import comments from previous analyses by using polyspace-comments-import, Polyspace copies any review information on the code metric Number of Paths in the previous result to this checker in the current result. If the current result contains the same code metric, the review information is copied to the code metric as well.

Risk

Violation of this checker might indicate that the function has too many possible execution paths. When there are too many execution paths, it might be difficult to test all paths exhaustively. Such functions are difficult to debug, test and maintain.

Fix

To fix this check, either refactor your code or change the checker threshold. When refactoring your code:

  • Avoid putting control structures in long sequences.

  • Split a complex function into multiple functions that are simpler and easy to test.

A best practice is to check the complexity of a module early in development to avoid costly post-development refactoring.

Examples

expand all

int afunc (int x);
int foo(int x,int y) //Noncompliant
{
	int flag;
	if (x <= 0){
		if (x > 10 ) { return 0; }
	}
	if (x<-240) {
		if (x < -2565) { 
			return (x < -253 ? 0: afunc (x <22566 ? 1: afunc(x < -25103 ? 0: 6))); 
		}
	}
	for (int i = 0; i< 10; i++)
	{
		while (x < y ) flag = 1;
		do {++x;} while (i<7);
		flag = 0;
	}
	return flag;
}

In this example, the function foo has many branching statements points, resulting in a path number of 45, which is greater than the specified threshold of 40. Because the function has many execution paths, testing the function is difficult and testing might fail to cover all execution paths. Polyspace flags the function as noncompliant.

Correction — Refactor Your Code

One possible correction is to split the function into two functions.

int afunc (int x);
int foo2(int x,int y)//Compliant 
{
	
	if (x <= 0){
		if (x > 10 ) { return 0; }
	}
	if (x<-240) {
		if (x < -2565) { 
			return (x < -253 ? 0: afunc (x <22566 ? 1: afunc(x < -25103 ? 0: 6))); 
		}
	}
}

int bar(int x,int y){//Complaint
	int flag;
	for (int i = 0; i< 10; i++)
	{
		while (x < y ) flag = 1;
		do {++x;} while (i<7);
		flag = 0;
	}
	return flag;
}

The functions foo2 and bar have acceptable number of paths and are easier to test compared to foo.

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC15
Default Threshold: 80

Version History

Introduced in R2021a