Main Content

Number of function calls exceeds threshold

The number of function calls in a function is greater than the defined call occurrence threshold of a function

Since R2021a

Description

This defect is raised when the body of a function contains more calls to functions than the specified call occurrence threshold of a function. For details about how Polyspace® calculates the number of function call occurrences, see Number of Call Occurrences

Polyspace uses the default threshold 50 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 Call Occurrences 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 performs too many tasks.

  • The function might have high interdependency with multiple other functions.

  • Changes in some other function might have unexpected impact on the flagged function.

These factors make the function difficult to maintain and debug.

Fix

To fix this check, either refactor your code or change the checker threshold. When refactoring the code, design the functions in your code so that:

  • Each function performs one specific task.

  • The functions have minimal side effects on other functions.

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

Examples

expand all

 long long power(double x, int n){
	 long long BN = 1;
	 for(int i = 0; i<n;++i){
		 BN*=x;
	 }
	 return BN;
 }
long long factorial(int n){
	 long long BN = 1;
	 for(int i = 1; i<=n;++i){
		 BN*=i;
	 }
	 return BN;
 }
 
 double AppxIndex(double m, double f){//Noncompliant
	 double U = (power(m,2) - 1)/(power(m,2)*factorial(2));
	 double V = (power(m,4) + 27*power(m,2)+38)/factorial(3)*(2*power(m,2)+3);
	 return (1+2*f*power(U,2)*(1+power(m,2)*factorial(static_cast<int>(U/V))
       + power(m,3)/power(m,3)*(U-V)))/( (1-2*f*power(U,2)*(1+power(m,2)*
        factorial(static_cast<int>(U/V)) + power(m,3)/power(m,3)*(U-V)))); }

The function AppxIndex contains 17 different function calls, which is greater than the specified call occurrence threshold of 10. Such high call occurrence indicate that this function is doing too many tasks. It also indicates that the function has high interdependencies with other functions. These factor make AppIndex difficult to maintain, test, or debug.

Correction — Refactor Code

One possible correction is to refactor the function. Here, the function is split into smaller functions that performs specific tasks.

  long long power(double x, int n){//Compliant
	long long BN = 1;
	for(int i = 0; i<n;++i){
		BN*=x;
	}
	return BN;
}
long long factorial(int n){//Compliant
	long long BN = 1;
	for(int i = 1; i<=n;++i){
		BN*=i;
	}
	return BN;
}

double CalculateU(double m){//Compliant
	return (power(m,2) - 1)/(power(m,2)*factorial(2));
}

double CalculateV(double m){//Compliant
	return (power(m,4) + 27*power(m,2)+38)/factorial(3)*(2*power(m,2)+3);
}

double CalculateMid(double m, double f){//Compliant
	double U = CalculateU(m);
	double V = CalculateU(m); 
	return (2*f*power(U,2)*(1+power(m,2)*factorial(static_cast<int>(U/V))
             + power(m,3)/power(m,3)*(U-V)));
}

double AppxIndex(double m, double f){//Compliant
	
	return (1+CalculateMid(m,f))/( (1-CalculateMid(m,f)));
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC06
Default Threshold: 50

Version History

Introduced in R2021a