Halstead complexity measures


Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in 1977 as part of his treatise on establishing an empirical science of software development.
Halstead made the observation that metrics of the software should reflect the implementation or expression of algorithms in different languages, but be independent of their execution on a specific platform.
These metrics are therefore computed statically from the code.
Halstead's goal was to identify measurable properties of software, and the relations between them.
This is similar to the identification of measurable properties of matter and the relationships between them.
Thus his metrics are actually not just complexity metrics.

Calculation

For a given problem, Let:
From these numbers, several measures can be calculated:
The difficulty measure is related to the difficulty of the program to write or understand, e.g. when doing code review.
The effort measure translates into actual coding time using the following relation,
Halstead's delivered bugs is an estimate for the number of errors in the implementation.
Consider the following C program:

main

The unique operators are:
main, , , int, scanf,
&, =, +, /, printf, ,, ;
The unique operands are:
a, b, c, avg, "%d %d %d", 3, "avg = %d"