Main Content

Number of Instructions

Number of instructions per function

Description

Note

Use Bug Finder instead of Code Prover for computing code metrics. Support for computing code metrics in Code Prover will be removed in a future release. See Version History.

This metric measures the number of instructions in a function body.

The recommended upper limit for this metric is 50. For more modular code, try to enforce an upper limit for this metric.

To enforce limits on metrics, see Compute Code Complexity Metrics Using Polyspace.

Computation Details

The metric is calculated using these rules:

  • A simple statement ending with a semicolon (;) is one instruction. If the statement is empty, it is not counted as an instruction.

  • A variable declaration is counted as one instruction if either of these conditions are true:

    • The variable is nonstatic and it is initialized. For instance:

      int var = 0;
      Declarations of static variables are not counted as statements. These can be initialized at compile time in C++.

    • The variable is initialized by a function call, such as a constructor. For instance:

      class A {
      public:
      	A();
      	A(int in) {
      		/*..*/
      	}
      };
      //...
      A obj(5); //Calls A::A(int). This is considered a single instruction.

  • Control flow statements such as if, for, break, goto, return, switch, while, do-while are each counted as one instruction.

  • Beginning and ending braces of a block of code are not counted as instructions. This block is counted as one instruction:

    {
        int var = 1;
    }

  • Labels are not counted as instructions by themselves. This code shows two instructions. The case labels are not counted as instructions by themselves.

    switch (1) {  // Instruction 1: switch 
        case 0: 
        case 1: 
        case 2: 
        default: 
        break;    // Instruction 2: break 
     } 

Number of instruction is not calculated for implicit constructors that are generated by the compiler when needed. For example, if you declare a constructor as =default, Polyspace® does not calculate the number of instruction for this constructor.

Examples

expand all

int func(int *arr, int size) {
	int i, countPos = 0, countNeg = 0, countZero = 0;
	for(i = 0; i < size; i++) {
		if(arr[i] > 0)
			countPos++;
		else
			if(arr[i] == 0)
				countZero++;
			else
				countNeg++;
	}
}

In this example, the Polyspace reports that the metric Number of Instruction for func has a value of nine. The instructions are:

  • countPos = 0

  • countNeg = 0

  • countZero = 0

  • for(i = 0;i < size; i++) { ... }

  • if(arr[i] >= 0)

  • countPos++

  • else if(arr[i] == 0)

    The ending else is counted as part of the if-else instruction.

  • countZero++

  • countNeg++

Note

This metric is different from the metric Number of Executable Lines. For instance:

  • for(i = 0;i < size; i++) has one instruction and one executable line.

  • This code has one instruction but three executable lines.

    for(i=0;
        i<size;
        i++)

This example shows how Polyspace calculates the number of instructions for a class. The class myClass contains three data members, two of which are initialized using nonstatic data member initializers. The default constructor of the class is declared as = default. The class also declares a user-defined nondefault constructor.

#include <cstdint>

class myClass
{
public:
	myClass(uint32_t &a) : num1(a) {}
	myClass() = default;

private:
	uint32_t num1;
	uint32_t num2{};
	uint32_t num3{};

};

The constructor myClass(uint32_t &a) initializes two data members using nonstatic data member initialization. Each of these initializations is counted as an instruction, but the initialization of num1 in the initializer list is not counted. For this constructor, the number of instructions is 2.

The default constructor is declared as = default. This default constructed is generated by the compiler if needed. Polyspace does not calculate the number of instruction for such compiler generated implicit constructors.

Metric Information

Group: Function
Acronym: STMT
HIS Metric: Yes

Version History

expand all