Misplaced Pages

Halstead complexity measures

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Software maintainability index

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 (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.

Calculation

For a given problem, let:

  • η 1 {\displaystyle \,\eta _{1}} = the number of distinct operators
  • η 2 {\displaystyle \,\eta _{2}} = the number of distinct operands
  • N 1 {\displaystyle \,N_{1}} = the total number of operators
  • N 2 {\displaystyle \,N_{2}} = the total number of operands

From these numbers, several measures can be calculated:

  • Program vocabulary: η = η 1 + η 2 {\displaystyle \eta =\eta _{1}+\eta _{2}\,}
  • Program length: N = N 1 + N 2 {\displaystyle N=N_{1}+N_{2}\,}
  • Calculated estimated program length: N ^ = η 1 log 2 η 1 + η 2 log 2 η 2 {\displaystyle {\hat {N}}=\eta _{1}\log _{2}\eta _{1}+\eta _{2}\log _{2}\eta _{2}}
  • Volume: V = N × log 2 η {\displaystyle V=N\times \log _{2}\eta }
  • Difficulty : D = η 1 2 × N 2 η 2 {\displaystyle D={\eta _{1} \over 2}\times {N_{2} \over \eta _{2}}}
  • Effort: E = D × V {\displaystyle E=D\times 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 = E 18 {\displaystyle T={E \over 18}} seconds

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

  • Number of delivered bugs : B = E 2 3 3000 {\displaystyle B={E^{2 \over 3} \over 3000}} or, more recently, B = V 3000 {\displaystyle B={V \over 3000}} is accepted.

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 {\displaystyle \,\eta _{1}} ) are: main, (), {}, int, scanf, &, =, +, /, printf, ,, ;

The distinct operands ( η 2 {\displaystyle \,\eta _{2}} ) are: a, b, c, avg, "%d %d %d", 3, "avg = %d"

  • η 1 = 12 {\displaystyle \eta _{1}=12} , η 2 = 7 {\displaystyle \eta _{2}=7} , η = 19 {\displaystyle \eta =19}
  • N 1 = 27 {\displaystyle N_{1}=27} , N 2 = 15 {\displaystyle N_{2}=15} , N = 42 {\displaystyle N=42}
  • Calculated Estimated Program Length: N ^ = 12 × l o g 2 12 + 7 × l o g 2 7 = 62.67 {\displaystyle {\hat {N}}=12\times log_{2}12+7\times log_{2}7=62.67}
  • Volume: V = 42 × l o g 2 19 = 178.4 {\displaystyle V=42\times log_{2}19=178.4}
  • Difficulty: D = 12 2 × 15 7 = 12.85 {\displaystyle D={12 \over 2}\times {15 \over 7}=12.85}
  • Effort: E = 12.85 × 178.4 = 2292.44 {\displaystyle E=12.85\times 178.4=2292.44}
  • Time required to program: T = 2292.44 18 = 127.357 {\displaystyle T={2292.44 \over 18}=127.357} seconds
  • Number of delivered bugs: B = 2292.44 2 3 3000 = 0.05 {\displaystyle B={2292.44^{2 \over 3} \over 3000}=0.05}

See also

References

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

External links

Category: