Main Content

Language scope exceeds threshold

The language scope of a function is greater than the defined threshold

Since R2021a

Description

Language scope represents the cost of maintaining or updating a function. For instance, if an operand occurs many times in a function, then changing the operand name is costly. The language scope of such a function would be high. This defect is raised when the language scope of a function exceeds the defined threshold. For details about how Polyspace calculates language scope, see Language Scope.

Note

The language scope calculated by Polyspace® is a floating point number. In this checker, the floating point language scope is converted to an integer by rounding it to the second decimal place and then multiplying it by 100.

Polyspace uses the default threshold 400 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 Language Scope 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:

  • Change to an operand might require many change in the function.

  • The function might be performing multiple tasks at once.

  • The function might have high degree of interdependency with other functions.

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

 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){//Noncompliant
	 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, In this function, the operand power is repeated many times. Any change to power requires careful examination of the function to make sure unexpected behaviors are not introduced. The language scope of the function AppxIndex is700, which is above the default language scope of 400. Polyspace flags the function as noncompliant

Correction — Refactor the Code

One possible correction is to refactor the code so that operands are not repeated many times. In this example, the function AppxIndex is refactored so that unrelated tasks are performed by independent functions. These functions are compliant.

// This code calculates effective index of materials  as decribed in
// the formula in 10.1364...
// power(x,n) returns the nth power of x (x^n)
// n is an integer
// x is a double
// return type is long long
long long power(double x, int n){//Compliant
	long long BN = 1;
	for(int i = 0; i<n;++i){
		BN*=x;
	}
	return BN;
}
// CalculateU(m) calculates the first intermediate variable
// required to calculate  polarization
// m is the relative refractive index
// return type is double;
double CalculateU(double m){//Compliant
	return (power(m,2) - 1)/(power(m,2)+2);
}
// CalculateV(m) calculates the second intermediate variable
// required to calculate  polarization
// m is the relative refractive index
// return type is double;
double CalculateV(double m){//Compliant
	return (power(m,4) + 27*power(m,2)+38)/(2*power(m,2)+3);
}
// CalculateMid(m,f) calculates the large term present in both numerator and denominator
// of the effective index calculation
// m is the relative refractive index
// f is the fillfactor 
// return type is double;
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));
}
//AppxIndex(m,f) calculates the approximate effective index
// m is the relative refractive index
// f is the fillfactor
//return type is double
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: SC18
Default Threshold: 400

Version History

Introduced in R2021a