Main Content

Number of calling functions exceeds threshold

The number of distinct callers of a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of distinct callers of the function is greater than the defined checker threshold. For details about how Polyspace calculates the number of calling functions, see Number of Calling Functions.

Polyspace® uses the default threshold 5 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 Calling Functions 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:

  • Issues arising in the flagged function might propagate to have unexpected impact in many places.

  • The data flow in the file is convoluted and finding the root cause of an issue might be difficult.

  • The flagged function might be performing multiple tasks.

These factors make the module 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

void utilityFunc(){//Noncompliant
	//...
}

void task1(){
	utilityFunc();
	//...
}
void task2(){
	utilityFunc();
	//...
}
void task3(){
	utilityFunc();
	//...
}
void task4(){
	utilityFunc();
	//...
}
void task5(){
	utilityFunc();
	//...
}
void task6(){
	utilityFunc();
	//...
}
void task7(){
	utilityFunc();
	//...
}

In this example, the function utilityFunc is called by seven different functions. It might indicate that utilityFunc performs multiple tasks. An issue arising in utilityFunc might have unexpected impact in any of the seven function that calls utilityFunc. Such interdependency make the code difficult to maintain and debug. Polyspace flags the function utilityFunc as noncompliant.

Correction — Refactor the Function

One possible correction is to refactor the function. For instance, delegate the different tasks required by different tasks into two utility functions instead of one.

void utilityFuncA(){//Compliant
	//...
}
void utilityFuncB(){//Compliant
	//...
}

void task1(){
	utilityFuncA();
	//...
}
void task2(){
	utilityFuncA();
	//...
}
void task3(){
	utilityFuncA();
	//...
}
void task4(){
	utilityFuncB();
	//...
}
void task5(){
	utilityFuncB();
	//...
}
void task6(){
	utilityFuncB();
	//...
}
void task7(){
	utilityFuncB();
	//...
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC04
Default Threshold: 5

Version History

Introduced in R2021a