Halstead complexity measures

Short description: Software maintainability index

Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in 1977[1] 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 (like the volume, mass, and pressure of a gas) and the relationships between them (analogous to the gas equation). Thus his metrics are actually not just complexity metrics.

## Contents

* 1 Calculation
* 2 Example
* 3 See also
* 4 References
* 5 External links

## Calculation

For a given problem, let:

* η1 = the number of distinct operators
* η2 = the number of distinct operands
* N1 = the total number of operators
* N2 = the total number of operands

From these numbers, several measures can be calculated:

* Program vocabulary: η=η1+η2
* Program length: N=N1+N2
* Calculated estimated program length: N^=η1log2η1+η2log2η2
* Volume: V=N×log2η
* Difficulty : D=η12×N2η2
* Effort: E=D×V

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,

* Time required to program: T=E18 seconds

Halstead's delivered bugs (B) is an estimate for the number of errors in the implementation.

* Number of delivered bugs : B=E233000 or, more recently, B=V3000 is accepted.[1]

## Example

Consider the following C program:

main()

int a, b, c, avg;
scanf("%d %d %d", &a, &b, &c);
avg = (a+b+c)/3;
printf("avg = %d", avg);

The distinct operators (η1) are: `main`, `()`, `{}`, `int`, `scanf`, `&`, `=`, `+`, `/`, `printf`, `,`, `;`

The distinct operands (η2) are: `a`, `b`, `c`, `avg`, `"%d %d %d"`, `3`, `"avg = %d"`

* η1=12, η2=7, η=19
* N1=27, N2=15, N=42
* Calculated Estimated Program Length: N^=12×log212+7×log27=62.67
* Volume: V=42×log219=178.4
* Difficulty: D=122×157=12.85
* Effort: E=12.85×178.4=2292.44
* Time required to program: T=2292.4418=127.357 seconds
* Number of delivered bugs: B=2292.44233000=0.05

## See also

* Function point
* Cyclomatic complexity

## References

1. ↑ 1.0 1.1 Halstead, Maurice H. (1977). Elements of Software Science. Amsterdam: Elsevier North-Holland, Inc.. ISBN 0-444-00205-7.

## External links

* The Halstead metrics \- Extensive discussion on the calculation and use of Halstead Metrics in an object-oriented environment (with specific reference to Java).
* Calculation of Halstead metrics \- Measurement of Halstead Metrics.
* Explanation with a Sample Program \- Example (on Page 6 of the PDF)
* Script computing Halstead Metrics and using them for commented code detection
* IBM
* Calculator for computing Halstead metrics

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Halstead complexity measures. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Halstead_complexity_measures&oldid=298337"