Main Content

Call tree complexity exceeds threshold

The call tree complexity of a file is greater than the defined threshold

Since R2021a

Description

The call tree complexity of a file represents the complexity between different levels of its function call tree. Polyspace® calculates the call tree complexity of a file as:

Call tree Complexity = number of call occurrencesnumber of function definitions + 1(1)
This defect is raised when the calculated call tree complexity of a file exceeds the defined call tree complexity threshold of the file. For details about how Polyspace calculates call tree complexity, see Estimated Function Coupling

Polyspace uses the default threshold 20 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 Estimated Function Coupling 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 interdependency of the functions in the file is unacceptably high.

  • Changes in one function in the file might introduce bugs or unexpected behavior in other functions in the file.

  • Reusing a single function of the file might be difficult because of interdependencies with other functions.

These factors make the file difficult to maintain, test, 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

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

In this example, the function power is called many times in the function AppIndex. The high interdependency between these two functions results in a call tree complexity of 12, which is greater than the threshold 10. This high value implies that changes in power might require rewriting or revising AppIndex. Polyspace flags the file as noncompliant.

Correction — Design Functions to Perform One Specific Task

One possible correction is to refactor the code so that a function performs one specific task. In this code, the functions have a lower interdependency. For instance, CalculateU and CalculateV are completely independent of each other. By designing the functions to perform one specific task, isolating any unexpected behavior is easier. This code is easier to debug, test, and maintain.

//Compliant
 long long power(double x, int n){ 
	 long long BN = 1;
	 for(int i = 0; i<n;++i){
		 BN*=x;
	 }
	 return BN;
 }
 double CalculateU(double m){
	 return (power(m,2) - 1)/(power(m,2)+2);
 }
 double CalculateV(double m){//Compliant
	 return (power(m,4) + 27*power(m,2)+38)/(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)*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: SC03
Default Threshold: 20

Version History

Introduced in R2021a