Halstead complexity metrics were developed by the late Maurice Halstead as a means of determining a quantitative measure of complexity directly from the operators and operands in the module to measure a program module's complexity directly from source code.Among the earliest software metrics, they are strong indicators of code complexity.Halstead Science is an estimation technique to find out size, tiem and effort of a software
Halstead´s metrics is based on interpreting the source code as a sequence of tokens and classifying each token to be an operator or an operand.
First we need to compute the following numbers, given the program:
- n1 = the number of distinct operators
- n2 = the number of distinct operands
- N1 = the total number of operators
- N2 = the total number of operands
The number of unique operators and operands (n1 and n2) as well as the total number of operators and operands (N1 and N2) are calculated by collecting the frequencies of each operator and operand token of the source program.
From these numbers, following measures can be calculated:
Program length (N)
N = N1 + N2Vocabulary size (n)
n = n1 + n2Program volume (V)
The program volume (V) is the information contents of the program, measured in mathematical bits. It is calculated as the program length times the 2-base logarithm of the vocabulary size (n) :
V = N * log2(n) Halstead's volume (V) describes the size of the implementation of an algorithm. The computation of V is based on the number of operations performed and operands handled in the algorithm. Therefore V is less sensitive to code layout than the lines-of-code measures.
The volume of a function should be at least 20 and at most 1000. The volume of a parameterless one-line function that is not empty; is about 20. A volume greater than 1000 tells that the function probably does too many things.
The volume of a file should be at least 100 and at most 8000. These limits are based on volumes measured for files whose LOCpro and v(G) are near their recommended limits. The limits of volume can be used for double-checking.
Difficulty level (D)
The difficulty level or error proneness (D) of the program is proportional to the number of unique operators in the program.
D is also proportional to the ration between the total number of operands and the number of unique operands (i.e. if the same operands are used many times in the program, it is more prone to errors).
D = ( n1 / 2 ) * ( N2 / n2 ) D is also proportional to the ration between the total number of operands and the number of unique operands (i.e. if the same operands are used many times in the program, it is more prone to errors).
Program level (L)
The program level (L) is the inverse of the error proneness of the program.
I.e. a low level program is more prone to errors than a high level program.
L = 1 / D I.e. a low level program is more prone to errors than a high level program.
Effort to implement (E)
The effort to implement (E) or understand a program is proportional to the volume and to the difficulty level of the program.
E = V * D Estimated Program Length
According to Halstead, The first hypothesis of software science is that the length of a well
Structured program is a function only of the number of unique operators and operands.
the estimated lngth is denoted by N^
the estimated lngth is denoted by N^
N^= n1log2n1+n2log2n2
Amongst all the programs, the one that has minimal size is said to have the potential volume, V*. Halstead argued that the minimal implementation of any algorithm was through a reference to a procedure that had been previously written. The implementation of this algorithm would then require nothing more than invoking the procedure and supplying the operands for its inputs and output parameters.
V*= (2+n2*) log2( 2+n2*)
Estimated program Level/difficulty
Halstead offered an alternate formula that estimates the program level.
L^ =2n2/n1N2
Hence, D =1 /L^
Effort:
Halstead hypothesized that the effort required to implement a program increases as the size of the program increases. It takes more effort to implement a program at a lower level than the higher level.
E= V/L^
Time to implement (T)
John stroud suggested that human mind is capable of making a limited number of elementary discriminations per second. Stroud claims that β (stroud number) ranges between 5 to 20. Since Effort uses elementary discriminations as its unit of measure, the programming time would be: T = E / β
β is normally set to 18 since this seemed to give best results in Halstead earliest experiments.