Halstead's Software Metrics
Halstead's Software Metrics
Token Count
The size of the vocabulary of a program, which consists of the number of unique tokens used to
build a program is defined as:
n = n1+n2
Program Length
N=N1+N2
Example – List out the operators and operands and also calculate the values of software science
measures
int sort (int x[ ], int n)
{
int i, j, save, im1;
/*This function sorts array x in ascending order */
If (n< 2) return 1;
for (i=2; i< =n; i++)
{
im1=i-1;
for (j=1; j< =im1; j++)
if (x[i] < x[j])
{
Save = x[i];
x[i] = x[j];
x[j] = save;
}
}
return 0;
}
Explanation –
operators occurrences operands occurrences
int 4 sort 1
() 5 x 7
, 4 n 3
[] 7 i 8
if 2 j 7
< 2 save 3
; 11 im1 3
for 2 2 2
= 6 1 3
– 1 0 1
<= 2 – –
++ 2 – –
return 2 – –
{} 3 – –
One method for determining the amount of data is to count the number of entries in the cross-
reference list.
A variable is a string of alphanumeric characters that is defined by a developer and that is used
to represent some value during either compilation or execution.
1. Prepare a cross reference table of the program. A cross reference table consists of
having a column listing out the names of all the variables in a program. In each row, the line
numbers in which the variable at that row is found, are listed out.
1. #include <stdio.h>
2. int main()
3. {
4. int n, i;
5. unsigned long factorial = 1;
6.
7. printf("Enter an integer: ");
8. scanf("%d",&n);
9.
10. // show error if the user enters a negative integer
11. if (n < 0)
12. printf("Error! Factorial of a negative number doesn't exist.");
13.
14. else
15. {
16. for(i=1; i<=n; ++i)
17. {
18. factorial *= i; // factorial = factorial*i;
19. }
20. printf("Factorial of %d = %f", n, factorial);
21. }
22.
23. return 0;
24. }
3. Calculate the total count of live variables from step 2 of the counts of live variables on
each line of code in the program.
4. Count the number of executable lines of code in the program. Exclude the # directives,
function names, starting brackets, ending brackets of the program and declarations from a C
program.
It is thus possible to define the average number of live variables, LV which is the sum of the
count of live variables divided by the count of executable statements in a program. This is a
complexity measure for data usage in a procedure or program.
LV = 19/17.
Sharing of data among modules
Every system can be considered to include a set of modules which when combined together
form a complete system. Upon combining these modules, it is found that the modules need to
interact with each other. During interaction, modules usually pass data or control information
to other modules through messages. Each message may carry a certain amount of data, which
depends on the degree of coupling between modules. Thus modules become dependent on
each other as a result.
Also, the ripple effect is evident in this case. The ripple effect is an effect similar to still water in
a pond. IF you throw a stone into this pond, it will cause ripples throughout the surface of the
pond. Similarly, changes in a software cause ripples across the entire software system.
Another aspect is that there are two types of variables in a system. These are local and global
variables. Local variables are alive in a single module or unit. Thus, these are usually shared only
as parameters in a function call. On the other hand, global variables are available throughout
the system and can be modified by any module. Thus having global variables in a system causes
problems in maintaining and changing the system if required.
Thus, it is required that this dependency between modules may be measured using some
metrics. These are information flow metrics.
Every software system has components. A component may be defined as any element
identified by decomposing a software system into its constituent parts. Since components in a
system have been created out of the system therefore they are related to each other and
interact with each other. Thus, when a component calls other components, it is said to be
coupled with other components. Also, each component has to perform one or more functions
and a component is said to be cohesive in this context. Thus if a component performs only one
function, it is said to be highly cohesive.
Thus, to measure degree of coupling there are metrics called information flow metrics. These
metrics model the degree of cohesion and coupling for a particular system component.