Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mehen.ophi.dev/llms.txt

Use this file to discover all available pages before exploring further.

The Halstead metrics are a suite of measures derived purely from the operators and operands in a source file. Maurice Halstead proposed them in Elements of Software Science (1977) as a way to characterize program size, difficulty, effort, and bug count from token statistics alone — without running the code. mehen reports the canonical Halstead suite per space and per file.

What mehen emits

KeyTypeDefinition
halsteadfloatDefault surface; equals halstead.volume.
halstead.volumefloatV = N · log₂(η)
halstead.difficultyfloatD = (η₁ / 2) · (N₂ / η₂)
halstead.effortfloatE = D · V
halstead.vocabularyintη = η₁ + η₂
halstead.lengthintN = N₁ + N₂
halstead.n1intDistinct operators.
halstead.N1intTotal operators.
halstead.n2intDistinct operands.
halstead.N2intTotal operands.
halstead.bugsfloatB = V / 3000 (estimated delivered bugs).
halstead.timefloatT = E / 18 (estimated implementation time, seconds).
halstead.estimated_program_lengthfloatÑ = η₁ · log₂(η₁) + η₂ · log₂(η₂)
halstead.levelfloatL = 1 / D
halstead.purity_ratiofloatÑ / N

Definitions

TermMeaning
η₁Number of distinct operators in the program.
η₂Number of distinct operands.
N₁Total occurrences of operators.
N₂Total occurrences of operands.
The classical Halstead derived quantities follow:
η = η₁ + η₂                       (vocabulary)
N = N₁ + N₂                       (length)
V = N · log₂(η)                   (volume)
D = (η₁ / 2) · (N₂ / η₂)          (difficulty)
E = D · V                         (effort)
B = V / 3000                      (estimated bugs)
T = E / 18                        (estimated implementation time)
The constant 18 in the time formula is Halstead’s “Stroud number” — the number of mental discriminations per second a programmer is assumed to make.

Per-language operator/operand split

What counts as an operator vs. an operand is language-specific. mehen’s analyzers follow the prevailing convention:
  • Operators: keywords (if, for, return, …), arithmetic and logical symbols (+, &&, …), parentheses pair (), brackets [], and assignment operators.
  • Operands: identifiers, literal values (numbers, strings, booleans, null/undefined), and type names.
The exact mapping for each language lives in its analyzer crate at crates/mehen-<lang>/.

How to read it

HalsteadInterpretation
volume low, difficulty lowSmall, easy code.
volume high, difficulty lowLong but mechanical (e.g., big lookup table).
volume low, difficulty highCompact but tricky (clever one-liner).
volume high, difficulty highLarge and tricky — refactor candidate.
halstead.bugs and halstead.time are rough estimates with limited empirical backing. Treat them as order-of-magnitude signals, not promises.

References

  • Halstead, M. H. (1977). Elements of Software Science. Operating and Programming Systems Series. Elsevier. OSTI record.
  • Kearney, J. K., et al. Software Complexity Measurement — MIT lecture notes that summarize the Halstead operator/operand formulation alongside McCabe. PDF (MIT OCW 16.355).
  • Christensen, K., Fitsos, G. P. & Smith, C. P. (1981). A perspective on software science. IBM Systems Journal 20(4): 372–387. DOI.
  • Sonar: Halstead in the metrics definitions.
  • Radon: Halstead — Python reference implementation of the formulas.

See also