Main Content

Number of lines in function body exceeds threshold

The number of lines in 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 lines within the body of the function is greater than the defined checker threshold. For details about how Polyspace calculates the number of lines in the body of a function, see Number of Lines Within Body

Polyspace® uses the default threshold 1200 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 Lines Within Body 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 too long. Lengthy function are difficult to read and understand. Maintaining, testing and debugging lengthy functions might be costly in terms of resource and time.

Fix

To fix this check, either refactor your code or change the checker threshold. When refactoring the code, make the functions modular. That is, design your code so that each function performs one specific task with as little side effect as possible. Modular design of function make them easy to test, debug, and maintain. Modular functions also enables efficient code reuse and might reduce code duplication.

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 length of the function CalculateAppxIndex exceeds the defined threshold of 100.

Correction — Refactor the Code

One possible correction is to refactor the code so that a function perform one specific task. In this case, different tasks in the CalculateAppxIndex are delegated to other functions modular functions so that each functions performs one specific tasks.

#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 //Compliant
  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;
   
 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: SC10
Default Threshold: 1200

Version History

Introduced in R2021a