# Software Engineering | Halstead’s Software Metrics

Halstead’s Software metrics are a set of measures proposed by Maurice Halstead to evaluate the complexity of a software program. These metrics are based on the number of distinct operators and operands in the program and are used to estimate the effort required to develop and maintain the program.

### Field of Halstead Metrics

This is the total number of operator and operand occurrences in the program.**Program length (N):**This is the total number of distinct operators and operands in the program.**Vocabulary size (n):**This is the product of program length (N) and the logarithm of vocabulary size (n), i.e., V = N*log2(n).**Program volume (V):**This is the ratio of the number of operator occurrences to the number of operand occurrences in the program, i.e., L = n1/n2, where n1 is the number of operator occurrences and n2 is the number of operand occurrences.**Program level (L):**This is the ratio of the number of unique operators to the total number of operators in the program, i.e., D = (n1/2) * (N2/n2).**Program difficulty (D):**This is the product of program volume (V) and program difficulty (D), i.e., E = V*D.**Program effort (E):**This is the estimated time required to implement the program, based on the program effort (E) and a constant value that depends on the programming language and development environment.**Time to implement (T):**

Halstead’s software metrics can be used to estimate the size, complexity, and effort required to develop and maintain a software program. However, they have some limitations, such as the assumption that all operators and operands are equally important, and the assumption that the same set of metrics can be used for different programming languages and development environments.

Overall, Halstead’s software metrics can be a useful tool for software developers and project managers to estimate the effort required to develop and maintain software programs.

n1 = Number of distinct operators.

n2 = Number of distinct operands.

N1 = Total number of occurrences of operators.

N2 = Total number of occurrences of operands.

## Halstead Metrics

Halstead metrics are:

The total number of operator occurrences and the total number of operand occurrences.**Halstead Program Length:**

N = N1 + N2

And estimated program length is, N^{^}= n1log_{2}n1 + n2log_{2}n2

The following alternate expressions have been published to estimate program length:- N
_{J}= log_{2}(n1!) + log_{2}(n2!) - N
_{B}= n1 * log_{2}n2 + n2 * log_{2}n1 - N
_{C}= n1 * sqrt(n1) + n2 * sqrt(n2) - N
_{S}= (n * log_{2}n) / 2

- N
The total number of unique operators and unique operand occurrences.**Halstead Vocabulary:**

n = n1 + n2Proportional to program size, represents the size, in bits, of space necessary for storing the program. This parameter is dependent on specific algorithm implementation. The properties V, N, and the number of lines in the code are shown to be linearly connected and equally valid for measuring relative program size.**Program Volume:**

V = Size * (log_{2}vocabulary) = N * log_{2}(n)

The unit of measurement of volume is the common unit for size “bits”. It is the actual size of a program if a uniform binary encoding for the vocabulary is used. And error = Volume / 3000The potential minimum volume V* is defined as the volume of the most succinct program in which a problem can be coded.**Potential Minimum Volume:**

V* = (2 + n2*) * log_{2}(2 + n2*)

Here, n2* is the count of unique input and output parametersTo rank the programming languages, the level of abstraction provided by the programming language, Program Level (L) is considered. The higher the level of a language, the less effort it takes to develop a program using that language.**Program Level:**

L = V* / V

The value of L ranges between zero and one, with L=1 representing a program written at the highest possible level (i.e., with minimum size).

And estimated program level is L^{^}=2 * (n2) / (n1)(N2)This parameter shows how difficult to handle the program is.**Program Difficulty:**

D = (n1 / 2) * (N2 / n2)

D = 1 / L

As the volume of the implementation of a program increases, the program level decreases and the difficulty increases. Thus, programming practices such as redundant usage of operands, or the failure to use higher-level control constructs will tend to increase the volume as well as the difficulty.Measures the amount of mental activity needed to translate the existing algorithm into implementation in the specified program language.**Programming Effort:**

E = V / L = D * V = Difficulty * VolumeShows the algorithm implementation program language level. The same algorithm demands additional effort if it is written in a low-level program language. For example, it is easier to program in Pascal than in Assembler.**Language Level:**

L’ = V / D / D

lambda = L * V* = L^{2}* VDetermines the amount of intelligence presented (stated) in the program This parameter provides a measurement of program complexity, independently of the programming language in which it was implemented.**Intelligence Content:**

I = V / DShows time (in minutes) needed to translate the existing algorithm into implementation in the specified program language.**Programming Time:**

T = E / (f * S)

The concept of the processing rate of the human brain, developed by psychologist John Stroud, is also used. Stoud defined a moment as the time required by the human brain requires to carry out the most elementary decision. The Stoud number S is therefore Stoud’s moments per second with:

5 <= S <= 20. Halstead uses 18. The value of S has been empirically developed from psychological reasoning, and its recommended value for programming applications is 18.

Stroud number S = 18 moments / second

seconds-to-minutes factor f = 60

## Counting Rules for C Language

- Comments are not considered.
- The identifier and function declarations are not considered
- All the variables and constants are considered operands.
- Global variables used in different modules of the same program are counted as multiple occurrences of the same variable.
- Local variables with the same name in different functions are counted as unique operands.
- Functions calls are considered operators.
- All looping statements e.g., do {…} while ( ), while ( ) {…}, for ( ) {…}, all control statements e.g., if ( ) {…}, if ( ) {…} else {…}, etc. are considered as operators.
- In control construct switch ( ) {case:…}, switch as well as all the case statements are considered as operators.
- The reserve words like return, default, continue, break, size, etc., are considered operators.
- All the brackets, commas, and terminators are considered operators.
- GOTO is counted as an operator and the label is counted as an operand.
- The unary and binary occurrences of “+” and “-” are dealt with separately. Similarly “*” (multiplication operator) is dealt with separately.
- In the array variables such as “array-name [index]” “array-name” and “index” are considered as operands and [ ] is considered as operator.
- In the structure variables such as “struct-name, member-name” or “struct-name -> member-name”, struct-name, and member-name are taken as operands, and ‘.’, ‘->’ are taken as operators. Some names of member elements in different structure variables are counted as unique operands.
- All the hash directives are ignored.

** Example –** List out the operators and operands and also calculate the values of software science measures like

int sort (int x[ ], int n)

{

int i, j, save, im1;

/*This function sorts array x in ascending order */

If (n< 2) return 1;

for (i=2; i< =n; i++)

{

im1=i-1;

for (j=1; j< =im1; j++)

if (x[i] < x[j])

{

Save = x[i];

x[i] = x[j];

x[j] = save;

}

}

return 0;

}

**Explanation**

**Explanation**

Operators | Occurrences | Operands | Occurrences |
---|---|---|---|

int | 4 | sort | 1 |

() | 5 | x | 7 |

, | 4 | n | 3 |

[] | 7 | i | 8 |

if | 2 | j | 7 |

< | 2 | save | 3 |

; | 11 | im1 | 3 |

for | 2 | 2 | 2 |

= | 6 | 1 | 3 |

– | 1 | 0 | 1 |

<= | 2 | – | – |

++ | 2 | – | – |

return | 2 | – | – |

{} | 3 | – | – |

n1=14 | N1=53 | n2=10 | N2=38 |

Therefore,

N = 91

n = 24

V = 417.23 bits

N^ = 86.51

n2* = 3 (x:array holding integer

to be sorted. This is used both

as input and output)

V* = 11.6

L = 0.027

D = 37.03

L^ = 0.038

T = 610 seconds

**Advantages of Halstead Metrics**

**Advantages of Halstead Metrics**

- It is simple to calculate.
- It measures the overall quality of the programs.
- It predicts the rate of error.
- It predicts maintenance effort.
- It does not require a full analysis of the programming structure.
- It is useful in scheduling and reporting projects.
- It can be used for any programming language.
- Easy to use: The metrics are simple and easy to understand and can be calculated quickly using automated tools.
- Quantitative measure: The metrics provide a quantitative measure of the complexity and effort required to develop and maintain a software program, which can be useful for project planning and estimation.
- Language independent: The metrics can be used for different programming languages and development environments.
- Standardization: The metrics provide a standardized way to compare and evaluate different software programs.

## Disadvantages of Halstead Metrics

- It depends on the complete code.
- It has no use as a predictive estimating model.
- Limited scope: The metrics focus only on the complexity and effort required to develop and maintain a software program, and do not take into account other important factors such as reliability, maintainability, and usability.
- Limited applicability: The metrics may not be applicable to all types of software programs, such as those with a high degree of interactivity or real-time requirements.
- Limited accuracy: The metrics are based on a number of assumptions and simplifications, which may limit their accuracy in certain situations.

## FAQs on Halstead Metrics

### 1. What is Halstead’s Effort?

**Answer:**

It is simply a series of metrics that are used to determine the necessary efforts required for maintaining a program.

### 2. How we can calculate Halstead’s metrics?

**Answer:**

Halstead Length: N = N1+N2

Halstead Calculated Length (Nx): Nx = N1 * log2(n1) + n2* log2(n2)

Halstead Volume(V): V = N*log2(n)

Halstead Effort(E): E=V*D

## Please

Loginto comment...