Halstead Intelligent Content

Halstead intelligent content represents the algorithm complexity in a mostly language-agnostic way by counting the amount of operators (+, =, &&, … and all reserved words like if and for) and operands (values, variables, function names, …)

The exact formula to calculate this metric is quite complex because it tries to be programming language independent, and some languages are much more verbose than others:

n1 = amount of unique operators
n2 = amount of unique operand
N1 = total amount of operators
N2 = total amount of operands

N = N1 + N2 = program length
V = N * log2(n1 + n2) = program volume
L = (2 / n1) * (n2 / N2) = program level
D = 1 / L = program difficulty
I = V / D = intelligent content

The basics are very simple: the more operators and operands, the more complex a program is.
The more operators or operands, the bigger the program volume. More unique operators or fewer unique operands means higher difficulty. Given 2 different implementations of a same algorithm, the longer one will score higher.

echo 'How are you';
$array = ['how', 'are', 'you'];
$string = implode(' ', $array);
$string = ucfirst($string);
echo $string;

Both of the above snippets perform the exact same thing, but the second one is a bit more complex:

  • you have to know more about the environment (what do implode & ucfirst do)
  • there are more steps to reason about
  • there are more places where something could go wrong (e.g. implode argument order could change)