{
    "slug": "halstead_index",
    "term": "Halstead Maintainability Index",
    "category": "quality",
    "difficulty": "advanced",
    "short": "A composite metric (0–100) combining lines of code, cyclomatic complexity, and Halstead volume to estimate maintainability.",
    "long": "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.",
    "aliases": [
        "Halstead complexity measures",
        "software science metrics"
    ],
    "tags": [
        "metrics",
        "static-analysis",
        "quality"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cyclomatic_complexity",
        "cognitive_complexity"
    ],
    "prerequisites": [
        "cyclomatic_complexity",
        "cognitive_complexity",
        "code_metrics"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Halstead_complexity_measures"
    ],
    "bad_code": "// High Halstead volume — many operators and operands in one expression:\nfunction calc($a, $b, $c, $d, $e) {\n    return (($a + $b) * ($c - $d) / $e) % ($a * $c) + ($b - $e) * $d;\n    // Complex expression: high vocabulary, difficulty, and effort score\n}",
    "good_code": "// Halstead metrics — based on operators and operands in source code\n// Definitions:\n// η1 = distinct operators  (e.g. +, =, if, return)\n// η2 = distinct operands   (e.g. $total, $price, 0.2)\n// N1 = total operator occurrences\n// N2 = total operand occurrences\n\n// Calculated metrics:\n// Vocabulary   η  = η1 + η2\n// Length       N  = N1 + N2\n// Volume       V  = N * log2(η)        — information content\n// Difficulty   D  = (η1/2) * (N2/η2)  — mental effort\n// Effort       E  = D * V              — total effort to implement/understand\n\n// Example PHP function:\npublic function applyDiscount(float $price, float $rate): float {\n    return $price * (1 - $rate);\n}\n// Operators: function, float, float, float, return, *, -, ()  → ~6 distinct\n// Operands:  $price, $rate, 1                                 → 3 distinct\n// Low volume + difficulty = easy to understand and maintain\n\n// Tool:\n$ phpmetrics --report-html=report/ src/\n// Reports Halstead metrics per method and class\n// High Volume (>1000) or Difficulty (>30) → consider refactoring",
    "quick_fix": "Run phpmetrics to see Halstead complexity — high volume or difficulty scores signal functions that are doing too much and should be split",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/halstead_index",
        "html_url": "https://codeclaritylab.com/glossary/halstead_index",
        "json_url": "https://codeclaritylab.com/glossary/halstead_index.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Halstead Maintainability Index](https://codeclaritylab.com/glossary/halstead_index) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/halstead_index"
            }
        }
    }
}