Main Content

Number of executable lines in function body exceeds threshold

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

Polyspace® uses the default threshold 1000 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 Executable Lines 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 and performs multiple tasks.

  • The function is likely to develop unexpected issues. The chance of an unexpected error increases with more lines of executable code.

  • The function might contain unexpected or unplanned development.

These factors make the function 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 is reasonably concise.

  • 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

This example shows how Polyspace flags functions that have more executable lines that the defines threshold of executable lines. For this example, the threshold is defined at 50. The function CalculateAppxIndex has 116 executable line. The large number of executable lines indicate that the function is overly long. While length of a function is not a big problem by itself, overly long functions might be poorly designed, with complex data flow, many local variables, and performing many tasks. Polyspace flags the function.

#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;
}

Correction — Refactor Functions

One possible correction is to refactor the function so that separate tasks are delegated to separate functions.

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC11
Default Threshold: 1000

Version History

Introduced in R2021a