Halstead’s Software Metrics

Maurice Halstead introduced a software metric known as Halstead’s Software Metrics in 1977, which defines a computer program as a collection of tokens that can be classified as either operators or operands. These tokens represent the building blocks of a program and can be used to measure the program’s size, vocabulary, and complexity. By analyzing these metrics, software developers can gain insights into the program’s maintainability, testing effort, and overall quality. The use of Halstead’s Software Metrics has become widespread in the field of software engineering, as it provides a comprehensive approach to measuring software complexity and can help improve the software development process. In this context, this article will explore What are Halstead Metrics, Advantages of Halstead Metrics, and Disadvantages of Halstead Metrics, Example of Halstead’s Software Metrics and how it can be used in software engineering to improve the software development process.

Token Count

Halstead’s Software Metrics considers a computer program as a collection of unique tokens, which can be either operators or operands. The metrics are defined in terms of these basic symbols, and they are used to measure the program’s complexity and size. The measures used in Halstead’s Software Metrics are

  • n1, which is the count of unique operators
  • n2, which is the count of unique operands
  • N1, which is the count of total occurrences of operators
  • N2 which is the count of total occurrences of operands
  • The total size of the program can be expressed in terms of the total tokens used, which is represented as N = N1 + N2.

What are Halstead Metrics

Program Level (L)

In Halstead’s Software Metrics, program level (L) is a measure of the level of abstraction used in a program, and it ranges between zero and one.

A value of L=1 represents a program written at the highest possible level of abstraction, with minimum size. The program level is calculated as the ratio of the actual volume (V*) of a program to the idealized minimum volume (V) that would be required to express the same functionality at the highest level of abstraction.

The formula for calculating program level is L=V*/V. The higher the program level, the more abstract and concise the program is, which can indicate better design and higher quality. The program level can be used to compare programs of different sizes and to estimate the optimal size of a program for a given functionality.

Program Difficulty

The Halstead software metrics include program difficulty, which measures the error-proneness of a program based on the number of unique operators it contains.

This metric is calculated using the formula D = (n1/2) * (N2/n2), where n1 is the count of unique operators, N2 is the total number of operand occurrences, and n2 is the count of unique operands. The higher the number of unique operators in a program, the more difficult it is to write and maintain, making it more prone to errors.

Program Volume (V)

In Halstead’s Software Metrics, program volume (V) is a measure of a program’s size, and is expressed in bits. The volume is calculated based on the total number of tokens (N) used in the program and the logarithm to the base 2 of the number of unique tokens (n) used in the program. If a uniform binary encoding is used for the vocabulary, then the volume is the actual size of the program.

If a uniform binary encoding is used for the vocabulary, then the volume is the actual size of the program. The formula for calculating the program volume is V = N * log2n

Program volume is an important metric for measuring program complexity, and it can be used to identify potential problems in a software system that may require further analysis and improvement.

Halstead Program Length

Halstead’s first hypothesis states that the length of a well-structured program is only dependent on the unique operators and operands.

The total number of operators and operands used in the program can be expressed as N = N1 + N2.

The estimated program length is denoted by N^ and can be calculated using the following formula: N^ = n1log2n1 + n2log2n2, where n1 is the count of unique operators, and n2 is the count of unique operands.

There are several alternate expressions available for estimating program length:

Halstead Program Length Alternate Length

Programming Effort (E)

Programming effort (E) is measured in elementary mental discriminations. The programming effort can be expressed as the ratio of program volume (V) to program level (L), where L ranges between zero and one, with L=1 representing a program written at the highest possible level.

The program difficulty (D) is proportional to the number of unique operators in the program.

Thus, the programming effort can be calculated as the product of program volume and program difficulty, that is, E=V/L=D*V.

Size of Vocabulary (n)

The size of the vocabulary of a program refers to the number of unique tokens used to create the program.

  • This vocabulary can be broken down into two categories: operators and operands.
  • The number of unique operators is denoted by n1, while the number of unique operands is denoted by n2.

The total size of the vocabulary, represented by n, can be calculated by n1+n2.

Potential Minimum Volume

The Potential Minimum Volume, denoted as V*, refers to the smallest possible size a program could be in order to solve a problem using a specific vocabulary.

The formula to calculate V* is:

V* = (2 + n2*) * log2 (2 + n2*)

where n2* is the count of unique input and output parameters used in the program. By calculating V*, we can determine the minimum size required to solve a problem using the specified vocabulary.

Language Level

The Language level is a metric that demonstrates the level of implementation of the algorithm in the program’s language. When the same algorithm is written in a low-level programming language, it may require additional effort to implement. In comparison, it is easier to write a program in high-level programming languages such as Pascal than in low-level languages like Assembler.

The language level metric can be calculated as follows:

L’ = V / D / D

Another metric, lambda (λ), can be used to estimate the programming effort of the implementation. It is calculated by multiplying the estimated program level (L) and the potential minimum volume (V*) of the program.

In addition, a lambda can be expressed as the product of the square of L and V, i.e., λ = L^2 * V.

Counting Rules for C Language

  • Comments are not counted in the program.
  • Identifiers and function declarations are not counted in the program.
  • Global variables used in different modules of the same program are counted as multiple occurrences of the same variable.
  • All variables and constants used in the program are considered as operands.
  • Function calls are considered as operators in the program.
  • Local variables with the same name in different functions are counted as unique operands.
  • All control statements, such as if() {…}, if() {…} else {…}, do {…} while (), while () {…}, for () {…}, are counted as operators.
  • In control constructs like switch () {case: …}, switch and all the case statements are considered as operators.
  • All brackets, commas, and terminators are considered as operators in the program.
  • Reserved words like return, default, continue, break, sizeof, etc., are considered as operators.
  • GOTO is counted as an operator, and the label is counted as an operand.
  • The unary and binary occurrence of “+” and “-” is counted separately in the program. Similarly, “*” (multiplication operator) is counted separately.
  • In array variables such as “array-name [index],” “array-name,” and “index” are considered as operands, and [ ] is considered an operator.
  • In structure variables such as “struct-name, member-name” or “struct-name -> member-name,” struct-name, member-name are considered as operands, and ‘.’, ‘->’ are taken as operators. Some names of member elements in different structure variables are counted as unique operands.
  • All hash directives are ignored while counting.
int sort(int x[], int n) {
    int i, j, save, im1;
    /* The function sorts array x in ascending order */
    if (n < 2) {
        return 1;
    }
    for (i = 1; i <= n; i++) {
        im1 = i - 1;
        for (j = 0; j <= im1; j++) {
            if (x[i] < x[j]) {
                save = x[i];
                x[i] = x[j];
                x[j] = save;
            }
        }
    }
    return 0;
}

Operators:

  • for ( )
  • if ( )
  • <
  • =
  • []

Operands:

  • int
  • sort
  • x
  • n
  • i
  • j
  • save
  • im1

Using the counting rules, we can calculate the values of n, N, V, E, and λ:

n = number of unique operators + number of unique operands
= 6 + 8
= 14

N = total number of operators + total number of operands
= 23 + 8
= 31

V = N * log2(n)
= 31 * log2(14)
= 110.61 (rounded to two decimal places)

E = V / D / D
= V / (n / 2) * (N / n)
= 110.61 / (14 / 2) * (31 / 14)
= 23.36 (rounded to two decimal places)

λ = E * V*
= E * (2 + n2*) * log2(2 + n2*)
(Assuming no input/output parameters are used, n2* = 0)
= 23.36 * (2 + 0) * log2(2 + 0)
= 46.72

Advantages of Halstead Metrics

  • Halstead Metrics is applicable to any programming language.
  • Halstead Metrics is easy to calculate.
  • It can predict the occurrence rate of errors.
  • It provides an overall quality measurement of programs.
  • It is helpful in project scheduling and reporting.
  • It can estimate the maintenance effort required.
  • It does not require a full analysis of the programming structure.

Disadvantages of Halstead Metrics

  • It requires the entire code to be analyzed, which may not be feasible in large programs.
  • It may not be suitable as a predictive model for estimating development effort or cost.
  • It may not provide accurate results for some programming paradigms, such as object-oriented programming or functional programming.
  • The metrics can be affected by the coding style and programming practices of the developer, which may not reflect the true complexity of the program.
  • The metrics do not consider the quality of the code, such as modularity, reusability, or maintainability.

We have made sure to present this article in an easy way to make you understand the concept of Halstead’s Software Metrics. Follow our tutorials.freshersnow.com frequently to get more updates on Halstead’s Software Metrics in Software Engineering.