White Box Testing
Test your code
   Home      Halstead software science
 

Halstead's Software Science

Halstead software science measurements were introduced in 1977 by M.H.Halstead principally to estimate the programming effort. Halstead complexity based upon counts of operators and operands.
 
The measures of Halstead are based on
    n1 = number of unique or distinct operators appearing in that implementation
    n2 = number of unique or distinct operands appearing in that implementation
    N1 = total usage of all of the operators appearing in that implementation
    N2 = total usage of all of the operands appearing in that implementation
 

MeasureN (Length of a program): is the sum of the total number of operators and operands in the program
 
 N = N1 + N2
 
Measuren (Vocabulary of the program): is the sum of the number of unique operators and operands
 
 n = n1 + n2.
 
MeasureV (Volume of the program): is for the size of any implementation of any algorithm
 
 V = (N1 + N2) log2 (n1 + n2)
 V = N log2 (n)
 
MeasureD (Difficulty of the program): is proportional to number of unique operators and total usage of operands
 
 D = (n1*N2)/(2*n2)                                                                                                                   
MeasureE (Effort required to implement or understand the program): is directly proportional to difficulty and volume
 
 E = D * V
 
Measure B (Number of bugs expected in program): is proportional to effort
 
                                                             B = E0.667/3000
 
Measure T (Time should be taken to write the program): is proportional to effort
 
 T = E/18   seconds
 
 
See following example
 
int BinSearch (char *item, char *table[], int n)
{
int bot = 0;
int top = n - 1;
int mid, cmp;
while (bot <= top) {
mid = (bot + top) / 2;
if (table[mid] == item)
return mid;
else if (compare(table[mid], item) < 0)
top = mid - 1;
else
bot = mid + 1;
}
return -1; // not found
}
 
 

operator

occurrences

operands

occurrences

int

5

item

3

char*

2

table

2

BinSearch()

1

n

2

,

2

bot

4

[]

3

0

2

=

5

top

4

;

8

1

3

-

2

mid

6

while()

1

cmp

1

<=

1

-1

1

{}

1

 

 

+

2

 

 

()

4

 

 

/

1

 

 

if() elseif else

1

 

 

return

2

 

 

compare()

1

 

 

1

 

 

n1:18

N1:43

n2:10

N2:28

 
 
Measure N (Length of a program) :   N = N1 + N2.
                                                                     =  43 + 28
                                                                     =  71
 
 
Measure n (Vocabulary):               n = n1 + n2.
                                                              = 18 + 10
                                                              = 28
 
 
Measure V (Volume):                    V = (N1 + N2) log2 (n1 + n2)
                                                          V = N log2 (n).
                                                              = 71 log (28)
                                                              = 341.322205
 
 
Measure D (Difficulty):                  D = (n1*N2)/(2*n2)
  = 18*28/2*10
  = 25.2
                                                                                                                                                                            
Measure E (Effort):                        E = D * V
                                                              = 25.2*341.322205
                                                              = 8601.319566
 
 
Measure B (Bugs expected):         B = E0.667/3000
                                                              = .14
 
 
Measure T (Time):                         T = E/18   seconds
                                                              = 478 seconds
 
 
Advantages of Halstead :
  1. Do not require in-depth and control flow analysis of program.
  2. Predicts Effort, rate of error and time.
  3. Useful in scheduling projects.
Drawbacks of Halstead :
  1. It depends on usage of operator and operands in completed code.
  2. It has no use in predicting complexity of program at design level.

 

External References