Main Content

Number of called functions exceeds threshold

The number of distinct function calls within the body of a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of calls to distinct functions within its body is greater than the defined checker threshold. For details about how Polyspace calculates the number of called functions, see Number of Called Functions.

Polyspace® uses the default threshold 7 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 Called 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:

  • The function depends on many functions and changes to any one of these functions might result in an unexpected behavior.

  • The function performs too many tasks.

  • The module might contain unexpected or unplanned development.

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

class A{
	//..
};
class B{
	//...
};
void transform1(A &a, B &b){
	//...
}
void transform2(A &a, B &b){
	//...
}
void transform3(A &a, B &b){
	//...
}
void transform4(A &a, B &b){
	//...
}
void transform5(A &a, B &b){
	//...
}
void transform6(A &a, B &b){
	//...
}
void transform7(A &a, B &b){
	//...
}
void transform8(A &a, B &b){
	//...
}
void main(){//Noncompliant
	A a; 
	B b;
	transform1(a,b);
	transform2(a,b);
	transform3(a,b);
	transform4(a,b);
	transform5(a,b);
	transform6(a,b);
	transform7(a,b);
	transform8(a,b);
}

In this example, the function main() calls eight different functions which is greater than the defined threshold of function calls. Debugging main() is difficult because the issue might be caused by any of these eight functions. Polyspace flags the function main().

Correction — Design Functions to Perform Specific Tasks

One possible correction is design functions that perform specific tasks. In this case, the individual tasks are already delegated to different functions, but main() performs too may of these tasks. Dividing the task load of main between multiple functions might make the functions easier to debug, maintain, and test.

class A{
	//..
};
class B{
	//...
};
void transform1(A &a, B &b){
	//...
}
void transform2(A &a, B &b){
	//...
}
void transform3(A &a, B &b){
	//...
}
void transform4(A &a, B &b){
	//...
}
void transform5(A &a, B &b){
	//...
}
void transform6(A &a, B &b){
	//...
}
void transform7(A &a, B &b){
	//...
}
void transform8(A &a, B &b){
	//...
}
void FirstStep(A &a, B &b){
	transform1(a,b);
	transform2(a,b);
	transform3(a,b);
}
void SecondStep(A &a, B &b){
	transform4(a,b);
	transform5(a,b);
	transform6(a,b);
}
void ThirdStep(A &a, B &b){
	transform7(a,b);
	transform8(a,b);
}
void main(){//Compliant
	A a; 
	B b;
	FirstStep(a,b);
	SecondStep(a,b);
	ThirdStep(a,b);
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC05
Default Threshold: 7

Version History

Introduced in R2021a