Main Content

Number of statements exceeds threshold

The number of statements in a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of statements in the function is greater than the defined threshold of the checker. For details about how Polyspace calculates the number of statements in a function, see Number of Instructions.

Polyspace® uses the default threshold 50 unless you specify a threshold for the checker. 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 Instructions 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 purpose of the function is unclear.

  • The function performs multiple tasks.

  • The function has high interdependency with other modules.

  • The function contains 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.

  • Independent data and code are properly isolated and encapsulated.

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

Examples

expand all

 
#include <cmath>
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#define CREAL_T
typedef float real32_T;
typedef double real64_T;
typedef struct {
  real32_T re;
  real32_T im;
} creal32_T;

typedef struct {
  real64_T re;
  real64_T im;
} creal_T; 
// Function Declarations
static double rt_powd_snf(double u0, double u1);

void CalculateAppxIndex(double r, double epsilon_s, double epsilon_h, double //Noncompliant
  lambda, double f, creal_T *eps_eff, creal_T *mu_eff) 
{
  double n_h;
  double m;
  double a;
  double alpha_e_re;
  double alpha_e_im;
  double alpha_m_re;
  double alpha_m_im;
  int l;
  double br;

  //  sphere radius is 1 micron;
  //  the refractive index of inclusion
  n_h = std::sqrt(epsilon_h);

  //  the refractive index of host
  m = std::sqrt(epsilon_s) / n_h;
  n_h = 6.2831853071795862 * n_h * r / lambda;

  //  size parameter;
  a = (n_h + m) + 2.0;
  m = n_h - m;

  //  polarizability
  n_h = 6.2831853071795862 * rt_powd_snf(r, 3.0) / rt_powd_snf(n_h, 3.0);
  alpha_e_re = 0.0;
  alpha_e_im = 0.0;
  alpha_m_re = 0.0;
  alpha_m_im = 0.0;
  for (l = 0; l < 5; l++) {
    alpha_e_re += 0.0 * ((2.0 * (1.0 + (double)l) + 1.0) * a);
    alpha_e_im += (2.0 * (1.0 + (double)l) + 1.0) * a;
    alpha_m_re += 0.0 * ((2.0 * (1.0 + (double)l) + 1.0) * (m + 2.0));
    alpha_m_im += (2.0 * (1.0 + (double)l) + 1.0) * (m + 2.0);

    //  alpha = alpha + 1i* [(2*l+1)*(an(l) + bn(l))];
  }

  alpha_e_re *= n_h;
  alpha_e_im *= n_h;
  alpha_m_re *= n_h;
  alpha_m_im *= n_h;

  // alpha = aa*alpha;
  n_h = f / (4.1887902047863905 * rt_powd_snf(r, 3.0));
  alpha_e_re *= n_h;
  alpha_e_im *= n_h;
  alpha_m_re *= n_h;
  alpha_m_im *= n_h;
  if (alpha_e_im == 0.0) {
    m = alpha_e_re / 3.0;
    n_h = 0.0;
  } else if (alpha_e_re == 0.0) {
    m = 0.0;
    n_h = alpha_e_im / 3.0;
  } else {
    m = alpha_e_re / 3.0;
    n_h = alpha_e_im / 3.0;
  }

  br = 1.0 - m;
  m = 0.0 - n_h;
  if (m == 0.0) {
    if (alpha_e_im == 0.0) {
      m = alpha_e_re / br;
      alpha_e_im = 0.0;
    } else if (alpha_e_re == 0.0) {
      m = 0.0;
      alpha_e_im /= br;
    } else {
      m = alpha_e_re / br;
      alpha_e_im /= br;
    }
  } else {
    n_h = std::abs(m);
    if (br > n_h) {
      a = m / br;
      n_h = br + a * m;
      m = (alpha_e_re + a * alpha_e_im) / n_h;
      alpha_e_im = (alpha_e_im - a * alpha_e_re) / n_h;
    } else if (n_h == br) {
      if (br > 0.0) {
        a = 0.5;
      } else {
        a = -0.5;
      }

      if (m > 0.0) {
        n_h = 0.5;
      } else {
        n_h = -0.5;
      }

      m = alpha_e_re * a + alpha_e_im * n_h;
      alpha_e_im = alpha_e_im * a - alpha_e_re * n_h;
    } else {
      a = br / m;
      n_h = m + a * br;
      m = (a * alpha_e_re + alpha_e_im) / n_h;
      alpha_e_im = (a * alpha_e_im - alpha_e_re) / n_h;
    }
  }

  eps_eff->re = epsilon_h * (1.0 + m);
  eps_eff->im = epsilon_h * alpha_e_im;
  if (alpha_m_im == 0.0) {
    m = alpha_m_re / 3.0;
    n_h = 0.0;
  } else if (alpha_m_re == 0.0) {
    m = 0.0;
    n_h = alpha_m_im / 3.0;
  } else {
    m = alpha_m_re / 3.0;
    n_h = alpha_m_im / 3.0;
  }

  br = 1.0 - m;
  m = 0.0 - n_h;
  if (m == 0.0) {
    if (alpha_m_im == 0.0) {
      m = alpha_m_re / br;
      alpha_m_im = 0.0;
    } else if (alpha_m_re == 0.0) {
      m = 0.0;
      alpha_m_im /= br;
    } else {
      m = alpha_m_re / br;
      alpha_m_im /= br;
    }
  } else {
    n_h = std::abs(m);
    if (br > n_h) {
      a = m / br;
      n_h = br + a * m;
      m = (alpha_m_re + a * alpha_m_im) / n_h;
      alpha_m_im = (alpha_m_im - a * alpha_m_re) / n_h;
    } else if (n_h == br) {
      if (br > 0.0) {
        a = 0.5;
      } else {
        a = -0.5;
      }

      if (m > 0.0) {
        n_h = 0.5;
      } else {
        n_h = -0.5;
      }

      m = alpha_m_re * a + alpha_m_im * n_h;
      alpha_m_im = alpha_m_im * a - alpha_m_re * n_h;
    } else {
      a = br / m;
      n_h = m + a * br;
      m = (a * alpha_m_re + alpha_m_im) / n_h;
      alpha_m_im = (a * alpha_m_im - alpha_m_re) / n_h;
    }
  }

  mu_eff->re = 1.0 + m;
  mu_eff->im = alpha_m_im;
}

In this example, the function CalculateAppxIndex has more instructions than the default threshold of 50. Such high number of instructions indicate that the function has an ill-defined purpose and performs many tasks at once. The function is difficult to debug. Polyspace flags the function.

Correction — Delegate Independent Tasks to Functions

One possible correction is to refactor the function by delegating individual functions to smaller functions. Such modular design enables easier debugging and efficient code reuse.

#include <cmath>
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#define CREAL_T
typedef float real32_T;
typedef double real64_T;
typedef struct {
  real32_T re;
  real32_T im;
} creal32_T;

typedef struct {
  real64_T re;
  real64_T im;
} creal_T; 
// Function Declarations
static double rt_powd_snf(double u0, double u1);
void PolarizabilityE(double&, double&, double&,double& );
void PolarizabilityM(double&, double&, double&,double& );
void Eps_eff(double&,double&,creal_T*);
void Mu_eff(double&,double&,creal_T*);
void CalculateAppxIndex(double r, double epsilon_s, double epsilon_h, double
  lambda, double f, creal_T *eps_eff, creal_T *mu_eff)//Compliant
{
  double n_h;
  double m;
  double a;
  double alpha_e_re;
  double alpha_e_im;
  double alpha_m_re;
  double alpha_m_im;
  int l;
  double br;

  //  sphere radius is 1 micron;
  //  the refractive index of inclusion
  n_h = std::sqrt(epsilon_h);

  //  the refractive index of host
  m = std::sqrt(epsilon_s) / n_h;
  n_h = 6.2831853071795862 * n_h * r / lambda;

  //  size parameter;
  a = (n_h + m) + 2.0;
  m = n_h - m;
   
 PolarizabilityE(a,m,alpha_e_re,alpha_e_im);  
 PolarizabilityM(a,m,alpha_e_re,alpha_e_im); 
 Eps_eff(alpha_e_re,alpha_e_im, eps_eff);
 Mu_eff(alpha_e_re,alpha_e_im, mu_eff);
}

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC12
Default Threshold: 50

Version History

Introduced in R2021a