Halstead complexity measures: Difference between revisions

Content deleted Content added
No edit summary
Add: redlink for Maurice Howard Halstead
 
(44 intermediate revisions by 35 users not shown)
Line 1:
{{short description|Software maintainability index}}
'''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 t hatthat 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 pr opertiesproperties 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, Letlet:
* <math>\,\eta_1</math> = the number of distinct operators
* <math>\,\eta_2</math> = the number of distinct operands
Line 17 ⟶ 18:
* Program vocabulary: <math>\eta = \eta_1 + \eta_2 \,</math>
* Program length: <math>N = N_1 + 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>V = N \times \log_2 \eta </math>
* Difficulty : <math>D = { \eta_1 \over 2 } \times { N_2 \over \eta_2 } </math>
Line 28 ⟶ 29:
 
Halstead's delivered bugs (B) is an estimate for the number of errors in the implementation.
*Number of delivered bugs : <math>B = {E^{2 \over 3} \over 3000}</math> or, more recently, <math>B = {V \over 3000}</math> is accepted.<ref {{Citation needed||reasonname='By"Elements whom?'|date=Augustof 2013}}.Software Science" />
 
== Example ==
 
Let us considerConsider the following [[C (programming language)|C]] program:
 
<syntaxhighlight lang="c">
<pre>
main()
{
int a, b, c, avg;
scanf("%d %d %d", &a, &b, &c);
avg = (a + b + c) / 3;
printf("avg = %d", avg);
}
</syntaxhighlight>
</pre>
The uniquedistinct operators (<math>\,\eta_1</math>) are:
<ttcode>main</ttcode>, <ttcode>()</ttcode>, <ttcode>{}</ttcode>, <ttcode>int</ttcode>, <ttcode>scanf</ttcode>,
<ttcode>&</ttcode>, <ttcode>=</ttcode>, <ttcode>+</ttcode>, <ttcode>/</ttcode>, <ttcode>printf</ttcode>, <code>,</code>, <code>;</code>
The uniquedistinct operands (<math>\,\eta_2</math>) are:
<ttcode>a</ttcode>, <ttcode>b</ttcode>, <ttcode>c</ttcode>, <ttcode>avg</ttcode>, <ttcode>"%d %d %d"</ttcode>, <ttcode>3</ttcode>, <ttcode>"avg = %d"</ttcode>
* <math>\eta_1 = 1012</math>, <math>\eta_2 = 7</math>, <math>\eta = 1719</math>
* <math>N_1 = 1627</math>, <math>N_2 = 15</math>, <math>N = 3142</math>
* Calculated Estimated Program Length: <math>\hat{N} = 1012 \times log_2 1012 + 7 \times log_2 7 = 5262.967</math>
* Volume: <math>V = 3142 \times log_2 1719 = 126178.74</math>
* Difficulty: <math>D = { 1012 \over 2 } \times { 15 \over 7 } = 1012.785</math>
* Effort: <math>E = 1012.785 \times 126178.74 = 13552292.744</math>
* Time required to program: <math>T = { 1,3552292.744 \over 18 } = 75127.4357</math> seconds
* Number of delivered bugs: <math>B = { 1,3552292.744 ^ { 2 \over 3 } \over 3000 } = 0.0405</math>
 
== See also ==
Line 73 ⟶ 74:
* [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]]