Software Design · Software Engineering

Halstead Software Metrics

Watch lectures

Halstead Software Metrics

Definition

Halstead's Software Science Metrics (developed by Maurice Halstead, 1977) are a set of measures derived from counting operators and operands in source code, used to estimate program complexity, length, effort, and even development time.

Basic Counts

Symbol Meaning
n1 Number of distinct operators (e.g., +, if, while)
n2 Number of distinct operands (e.g., variable names, constants)
N1 Total occurrences of all operators
N2 Total occurrences of all operands

Derived Metrics (Formulas)

Metric Formula Meaning
Program Vocabulary (n) n = n1 + n2 Total distinct operators + operands
Program Length (N) N = N1 + N2 Total operator + operand occurrences
Calculated Length N̂ = n1·log₂(n1) + n2·log₂(n2) Estimated length based on vocabulary
Volume (V) V = N × log₂(n) Size of the implementation (in bits)
Difficulty (D) D = (n1/2) × (N2/n2) How difficult the program is to write/understand
Effort (E) E = D × V Mental effort required to develop the program
Time to Program (T) T = E / 18 (seconds) Estimated time to implement (18 = empirical constant)
Estimated Bugs (B) B = V / 3000 Estimated number of delivered bugs

Example (conceptual)

For a code snippet:

n1 = 4 distinct operators (e.g., =, +, if, while)
n2 = 3 distinct operands (e.g., x, y, total)
N1 = 10 total operator occurrences
N2 = 8 total operand occurrences

Plug into formulas above to compute Volume, Effort, etc.

Uses

  • Estimating program complexity without executing it (static analysis)
  • Predicting development effort/time
  • Estimating the likely number of bugs in delivered code
  • Comparing complexity across different implementations of the same function

Limitations

  • Based purely on counting tokens, ignores actual logical/control-flow complexity (unlike Cyclomatic Complexity)
  • The "18 seconds" and "3000" constants are empirical and may not generalize across all languages/domains

Exam tip: Memorize the formulas for Volume (V = N log₂n), Effort (E = D × V), and Bugs (B = V/3000) — numerical problems asking you to compute these from a given code snippet are very common.

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Software Engineering modules