Number of Instructions
Number of instructions per function
Description
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:
Declarations of static variables are not counted as statements. These can be initialized at compile time in C++.int var = 0;
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
Metric Information
Group: Function |
Acronym: STMT |
HIS Metric: Yes |