Halstead complexity measures are software metrics introduced by Maurice Howard Halstead in 1977[1] 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.

public class HeapSorter
{
private static int[] a;
private static int n;
public static void sort(int[] a0)
{
a=a0;
n=a.length;
heapsort();
}
private static void heapsort()
{
buildheap();
while (n>1)
{
n--;
exchange (0, n);
downheap (0);
}
}
private static void buildheap()
{
for (int v=n/2-1; v>=0; v--)
downheap (v);
}
private static void downheap(int v)
{
int w=2*v+1; // first descendant of v
while (w<n)
{
if (w+1<n) // is there a second descendant?
if (a[w+1]>a[w]) w++;
// w is the descendant of v with maximum label
if (a[v]>=a[w]) return; // v has heap property
// otherwise
exchange(v, w); // exchange labels of v and w
v=w; // continue
w=2*v+1;
}
}
private static void exchange(int i, int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
} // end class HeapSorter


## Example

Let us 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 unique operators are: main, (), {}, int, scanf, &, =, +, /, printf

The unique operands are: a, b, c, avg, "%d %d %d", 3, "avg = %d"

• $\eta_1 = 10$, $\eta_2 = 7$, $\eta = 17$
• $N_1 = 16$, $N_2 = 15$, $N = 31$
• Calculated Program Length: $\hat{N} = 10 \times log_2 10 + 7 \times log_2 7 = 52.9$
• Volume: $V = 31 \times log_2 17 = 126.7$
• Difficulty: $D = { 10 \over 2 } \times { 15 \over 7 } = 10.7$
• Effort: $E = 10.7 \times 126.7 = 1,355.7$
• Time required to program: $T = { 1,355.7 \over 18 } = 75.3$ seconds
• Number of delivered bugs: $B = { 1,355.7 ^ { 2 \over 3 } \over 3000 } = 0.04$

## References

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