Halstead complexity measures: Difference between revisions

Content deleted Content added
Dekart (talk | contribs)
No edit summary
Add: redlink for Maurice Howard Halstead
 
(112 intermediate revisions by 82 users not shown)
Line 1:
{{short description|Software maintainability index}}
'''Halstead complexity metric''' is a [[software metric]] indroduced by Maurice Howard Halstead in 1977. This metric is computed statically, without program execution.
'''Halstead complexity measures''' are [[software metric]]s introduced by [[Maurice Halstead|Maurice Howard Halstead]] in 1977<ref name="Elements of Software Science">{{cite book |author=Halstead, Maurice H. |title=Elements of Software Science |publisher=Elsevier North-Holland, Inc. |___location=Amsterdam |year=1977 |isbn=0-444-00205-7}}</ref> 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 [[ideal gas law|gas equation]]).
Thus his metrics are actually not just complexity metrics.
 
== Calculation ==
For a given problem, let:
Let's assume the following:
* <math>n1\,\eta_1</math> = the number of distinct operators
* <math>n2\,\eta_2</math> = the number of distinct operands
* <math>N1\,N_1</math> = the total number of operators
* <math>N2\,N_2</math> = the total number of operands
 
From these numbers, fiveseveral measures arecan derivedbe calculated:
* Program vocabulary: <math>n\eta = n1\eta_1 + n2 \eta_2 \,</math>
* Program length: <math>N = N1N_1 + N2 N_2 \,</math>
* Calculated estimated program length: <math>\hat{N} = \eta_1 \log_2 \eta_1 + \eta_2 \log_2 \eta_2 </math>
* Volume: <math>HVV = N \times \log_2 n\eta </math>
* Difficulty : <math>D = { n_1\eta_1 \over 2 } \times { N_2 \over n_2\eta_2 } </math>
* Effort: <math>E = D *\times V \,</math>
 
WhereThe Ddifficulty =measure is related to the difficulty of the program to write or understand, e.g. withwhen onedoing [[Codecode review]] (?).
*Program length: <math>N= N1 + N2 \,</math>
*Program vocabulary: <math>n= n1 + n2 \,</math>
*Volume: <math>V= N * (LOG2 n) \,</math>
*Difficulty : <math>D= (n1/2) * (N2/n2) \,</math>
*Effort: <math>E= D * V \,</math>
 
The effort measure translates into actual coding time using the following relation,
* Time required to program: <math>T = {E \over 18}</math> seconds
 
Halstead's delivered bugs (B) is an estimate for the number of errors in the implementation.
* Number of the used different operators ((<math>n_1</math>) and operand (<math>n_2</math>), ), together the vocabulary size <math>n</math>.
* Number of thedelivered altogetherbugs used: operators (<math>N_1B = {E^{2 \over 3} \over 3000}</math>) andor, operandmore recently, (<math>N_2B = {V \over 3000}</math>), togetheris theaccepted.<ref implementationname="Elements lengthof Software Science" <math>N</math>.
 
== Example ==
From this then the sizes of Halstead length become (<math>HL</math>) and Healstead volume (<math>HV=V</math>) are calculated:
* <math>HL = n_1 \times \log_2 n_1 + n_2 \times \log_2 n_2</math>
* <math>HV = N \times \log_2 n</math>
 
Consider the following [[C (programming language)|C]] program:
From the base quantities one can compute different characteristic numbers, e.g.:
 
* <math>D = { n_1 \over 2 } \times { N_2 \over n_2 }</math>
<syntaxhighlight lang="c">
main()
{
int a, b, c, avg;
scanf("%d %d %d", &a, &b, &c);
avg = (a+b+c)/3;
printf("avg = %d", avg);
}
</syntaxhighlight>
The distinct operators (<math>\,\eta_1</math>) are:
Where D = difficulty of the program to write or understand, e.g. with one [[Code review]] (?).
<code>main</code>, <code>()</code>, <code>{}</code>, <code>int</code>, <code>scanf</code>,
<code>&</code>, <code>=</code>, <code>+</code>, <code>/</code>, <code>printf</code>, <code>,</code>, <code>;</code>
The distinct operands (<math>\,\eta_2</math>) are:
<code>a</code>, <code>b</code>, <code>c</code>, <code>avg</code>, <code>"%d %d %d"</code>, <code>3</code>, <code>"avg = %d"</code>
* <math>\eta_1 = 12</math>, <math>\eta_2 = 7</math>, <math>\eta = 19</math>
* <math>N_1 = 27</math>, <math>N_2 = 15</math>, <math>N = 42</math>
* Calculated Estimated Program Length: <math>HL\hat{N} = n_112 \times \log_2 n_112 + n_27 \times \log_2 n_27 = 62.67</math>
* Volume: <math>V = N42 *\times (LOG2log_2 n)19 = \,178.4</math>
* Difficulty: <math>D = { 12 \over 2 } \times { 15 \over 7 } = 12.85</math>
* Effort: <math>E = 12.85 \times 178.4 = 2292.44</math>
* Time required to program: <math>T = { 2292.44 \over 18 } = 127.357</math> seconds
* Number of delivered bugs: <math>B = { 2292.44 ^ { 2 \over 3 } \over 3000 } = 0.05</math>
 
==see See also ==
From http://www.sei.cmu.edu/str/descriptions/halstead.html [http://www.sei.cmu.edu/str/descriptions/halstead.html] and the German wikipedia entry.
 
==see also==
* [[Function point]]
* http://de.wikipedia.org/wiki/Halstead-Metrik [http://de.wikipedia.org/wiki/Halstead-Metrik]
* [[Cyclomatic complexity]]
 
==References==
{{Reflist}}
 
==External links==
* [http://www.virtualmachinery.com/sidebar2.htm The Halstead metrics] - Extensive discussion on the calculation and use of Halstead Metrics in an object-oriented environment (with specific reference to Java).
* [http://www.verifysoft.com/en_halstead_metrics.html Calculation of Halstead metrics] - Measurement of Halstead Metrics.
* [http://www.win.tue.nl/~aserebre/2IS55/2010-2011/10.pdf Explanation with a Sample Program] - Example (on Page 6 of the PDF)
* [https://github.com/dborowiec/commentedCodeDetector Script computing Halstead Metrics and using them for commented code detection]
* [http://pic.dhe.ibm.com/infocenter/rtrthelp/v8r0m0/index.jsp?topic=%2Fcom.ibm.rational.testrt.studio.doc%2Ftopics%2Fcsmhalstead.htm IBM]
* [https://www.vcalc.com/wiki/halstead%20software%20complexity Calculator for computing Halstead metrics]
[[Category:Software metrics]]
[[de:Halstead-Metrik]]