Main Content

Number of function parameters exceeds threshold

The number of arguments of a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of its arguments is greater than the defined checker threshold. For details about how Polyspace calculates the number of parameters, see Number of Function Parameters

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 Function Parameters 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 indicates that:

  • The function might have unacceptably high degree of dependence on other functions.

  • The function might be performing more than one specific task. A best practice is to delegate one specific task to one function.

  • The function might contain unexpected or unplanned development.

  • The function might hinder performance because registers cannot hold all parameters.

These factors make the function difficult to maintain and debug.

Fix

To fix this check, either refactor your code or change the threshold in the checker selection XML. You might want to split the function into smaller chunks that performs a specific task and does not take more than the number of parameters specified as threshold. If the parameters of a functions are related, you might consider bundling them into structures.

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

Examples

expand all

#include<vector>
#include<complex>
#define PI 3.1416
std::vector<std::complex<double> >CalculateCoefficient(double, double, int); 
double CalculateMfactor(double, double);
std::complex<double> CalculateEffectiveIndex(//Noncompliant
                    double Radius, double Index1, double Index2,
                     double Wavelength, double FillFactor,
                     int Cutoff){
	double RelativeIndex = Index1/Index2;
	double SizeParameter = 2*PI*Radius*Index1/Wavelength;
	std::complex<double> Polarization = 0; 
	std::vector<std::complex<double>> Coefficient= 
          CalculateCoefficient(RelativeIndex,SizeParameter,Cutoff);
		//...
	 for (const auto& z : Coefficient){
		 Polarization += z;
	 }
	 double Multiplier = CalculateMfactor(FillFactor,Radius);
	 std::complex<double> Neff = (1.0 + Multiplier*Polarization)/
                (1.0 - Multiplier*Polarization);
	 return Neff;
}

In this example, the function CalculateEffectiveIndex takes six parameters, which is greater than the defined threshold five. The high number of parameter indicate that the function might be performing more than one specific task, and it might have high degree of dependencies with other functions. Polyspace flags the function as noncompliant.

Correction — Refactor The Code so that Functions Perform One Specific Task

One possible correction is to refactor the function so that it performs one specific task. In this code, the function CalculateEffectiveIndex is refactored into two smaller functions, each of which performs one specific task. The functions are easier to debug and maintain because they take less parameters than the defined threshold. Polyspace does not flag these functions.

#include<vector>
#include<complex>
#define PI 3.1416
std::vector<std::complex<double>>CalculateCoefficient(double, double, int); 
double CalculateMfactor(double, double);

std::complex<double> CalculatePolarization(//Compliant
        double Radius, double Index1, double Index2,
        double Wavelength, int Cutoff){
	double RelativeIndex = Index1/Index2;
	double SizeParameter = 2*PI*Radius*Index1/Wavelength;
	std::complex<double> Polarization = 0; 
	std::vector<std::complex<double> > Coefficient= 
              CalculateCoefficient(RelativeIndex,SizeParameter,Cutoff);
		//...
	 for (const auto& z : Coefficient){
		 Polarization += z;
	 }
	 
	 return Polarization;
}

std::complex<double> CalculateEffectiveIndex
      (std::complex<double> Polarization, double Multiplier){//Compliant
	 
	 std::complex<double> Neff = (1.0 + Multiplier*Polarization)/
              (1.0 - Multiplier*Polarization);
	 return Neff;
}

Check Information

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

Version History

Introduced in R2021a