Main Content

Number of local non-static variables exceeds threshold

The number of local nonstatic variables in a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of local nonstatic variables in the function is greater than the defined checker threshold. For details about how Polyspace calculates the number of local nonstatic variables in a function, see Number of Local Non-Static Variables.

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 Number of Local Non-Static Variables 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 is overly long.

  • The function performs many tasks rather than one specific task.

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 your code:

  • Consider splitting a function into smaller modules that performs a specific task.

  • Consider bundling variables that have a similar role into containers such as classes, structures, vectors, or maps.

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

Examples

expand all

#include <string>
void foo(void){//Noncompliant
	
	int Value1,Value2,Value3,Value4,Value5,Value6,Value7,Value8,Value9,Value10,Value11;
	std::string Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8,Name9,Name10,Name11;
	//....
	
}

In this example, The names and associated value of eleven separate entities are declared as separate variables. it is difficult to keep track of such a high number of variables. If you need to include more entities in the code, you need to declare more variables. This kind of code is difficult to test, maintain, and debug. Polyspace flags the function.

Correction — Bundle Related Data into Containers

One possible correction is to bundle the related data into containers. For instance, in this code the data is bundled into a std::map container, which is then wrapped in a class. Then in the code, you need to declare only one local variable and use the variable to store, manage, and access the data. This code is easier to maintain, test, and debug.

#include <string>
#include <map>
class Instance{
	public:
	Instance();
	Instance(int, std::string);
	protected:
	addData(int,std::string);
	getValue(std::string);
	//...
	private:
	std::map<std::string, int> DataMap;
	
};
void foo(void){//Compliant
	
	Instance A;
	//....
	A.addData(5,"Name");
	
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC08
Default Threshold: 20

Version History

Introduced in R2021a