Halstead Maintainability Index
Also Known As
Halstead complexity measures
software science metrics
TL;DR
A composite metric (0–100) combining lines of code, cyclomatic complexity, and Halstead volume to estimate maintainability.
Explanation
The Maintainability Index was developed at Hewlett-Packard and later adopted by Microsoft's Visual Studio. It combines: Lines of Code (volume), Cyclomatic Complexity, and Halstead Volume (derived from the number of distinct operators and operands). A score above 85 indicates highly maintainable code, 65–85 is moderate, below 65 is difficult to maintain. It is a rough heuristic — treat it as a relative indicator rather than an absolute measure.
Common Misconception
✗ Halstead metrics are purely theoretical and not used in practice. They form part of the Maintainability Index used by Visual Studio, SonarQube, and PHP tools — a low MI score reliably flags files that developers find hardest to modify.
Why It Matters
Halstead metrics quantify program vocabulary, length, and difficulty from operator/operand counts — they predict development effort and maintenance cost independently of test coverage or style.
Common Mistakes
- Using Halstead metrics in isolation rather than alongside cyclomatic complexity and lines of code.
- Not including Halstead difficulty in your quality gate — it catches complex expressions that low cyclomatic complexity misses.
- Optimising for Halstead metrics by reducing variable names — shorter names lower vocabulary at the cost of readability.
- Not realising that high Halstead volume often indicates a function that should be split.
Code Examples
✗ Vulnerable
// High Halstead volume — many operators and operands in one expression:
function calc($a, $b, $c, $d, $e) {
return (($a + $b) * ($c - $d) / $e) % ($a * $c) + ($b - $e) * $d;
// Complex expression: high vocabulary, difficulty, and effort score
}
✓ Fixed
// Halstead metrics — based on operators and operands in source code
// Definitions:
// η1 = distinct operators (e.g. +, =, if, return)
// η2 = distinct operands (e.g. $total, $price, 0.2)
// N1 = total operator occurrences
// N2 = total operand occurrences
// Calculated metrics:
// Vocabulary η = η1 + η2
// Length N = N1 + N2
// Volume V = N * log2(η) — information content
// Difficulty D = (η1/2) * (N2/η2) — mental effort
// Effort E = D * V — total effort to implement/understand
// Example PHP function:
public function applyDiscount(float $price, float $rate): float {
return $price * (1 - $rate);
}
// Operators: function, float, float, float, return, *, -, () → ~6 distinct
// Operands: $price, $rate, 1 → 3 distinct
// Low volume + difficulty = easy to understand and maintain
// Tool:
$ phpmetrics --report-html=report/ src/
// Reports Halstead metrics per method and class
// High Volume (>1000) or Difficulty (>30) → consider refactoring
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
41
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Ahrefs 10
Perplexity 9
Amazonbot 8
Unknown AI 3
SEMrush 2
Majestic 1
ChatGPT 1
Google 1
Also referenced
How they use it
crawler 35
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Run phpmetrics to see Halstead complexity — high volume or difficulty scores signal functions that are doing too much and should be split
📦 Applies To
any
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Functions with high Halstead volume or difficulty in phpmetrics output; complex functions with many unique operators and operands
Auto-detectable:
✓ Yes
phpmetrics
pdepend
sonarqube
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update