{
    "slug": "abc_metric",
    "term": "ABC Metric (Assignments, Branches, Conditions)",
    "category": "quality",
    "difficulty": "advanced",
    "short": "Code complexity metric using vector magnitude of assignments, branches, and conditions — more expressive than line count.",
    "long": "The ABC metric (Jerry Fitzpatrick, 1997) measures method complexity using three independent counts: A = assignments (state changes), B = branches (function/method calls and control transfers), and C = conditions (boolean expressions). The final score is computed as the vector magnitude: sqrt(A^2 + B^2 + C^2). Unlike cyclomatic complexity, which focuses on decision paths, ABC captures how much a method mutates state and delegates work. High A suggests excessive state manipulation, high B indicates heavy delegation or deep call chains, and high C reflects complex logic. Tools like PHPMetrics and PHPLoc report ABC per method. Scores above ~20 should be reviewed; above ~50 typically indicate methods that are difficult to test, reason about, and maintain.",
    "aliases": [
        "ABC score",
        "Assignment Branch Condition metric"
    ],
    "tags": [
        "metrics",
        "code-quality",
        "static-analysis"
    ],
    "misconception": "ABC metric and cyclomatic complexity measure the same thing. Cyclomatic complexity counts independent execution paths, while ABC measures state changes, calls, and conditions — they highlight different risks.",
    "why_it_matters": "High ABC scores correlate with fragile, hard-to-test code — reducing ABC improves readability, lowers defect rates, and simplifies refactoring.",
    "common_mistakes": [
        "Reducing ABC by splitting methods without reducing actual logic complexity.",
        "Ignoring high assignment counts — excessive state mutation is a major maintainability risk.",
        "Treating all branches equally — external calls and control flow both increase B but have different implications.",
        "Using ABC thresholds blindly without considering domain complexity."
    ],
    "when_to_use": [
        "Identifying overly complex or state-heavy methods.",
        "Enforcing maintainability thresholds in CI pipelines."
    ],
    "avoid_when": [
        "Evaluating very small methods where ABC adds little insight.",
        "Comparing across different domains without normalising expectations."
    ],
    "related": [
        "cyclomatic_complexity",
        "cognitive_complexity",
        "maintainability_index"
    ],
    "prerequisites": [],
    "refs": [
        "https://en.wikipedia.org/wiki/ABC_Software_Metric"
    ],
    "bad_code": "// ❌ High ABC — excessive state + branching\nfunction processOrder($order, $user, $coupon, $tax) {\n    $total = 0; $discount = 0; $shipping = 0;\n\n    if ($order && $user && $coupon) {\n        if ($coupon->valid && $coupon->amount > 0) {\n            $discount = $coupon->amount;\n        }\n\n        $total = $order->subtotal - $discount + $tax + $shipping;\n    }\n\n    return $total;\n}",
    "good_code": "// ✅ Lower ABC via decomposition and reduced state\nfunction processOrder($order, $user, $coupon, $tax) {\n    if (!$this->isProcessable($order, $user, $coupon)) {\n        return 0;\n    }\n\n    $discount = $this->calculateDiscount($coupon);\n\n    return $this->calculateTotal($order, $discount, $tax);\n}\n\nprivate function isProcessable($order, $user, $coupon): bool {\n    return $order && $user && $coupon;\n}\n\nprivate function calculateDiscount($coupon): float {\n    return ($coupon->valid && $coupon->amount > 0)\n        ? $coupon->amount\n        : 0;\n}\n\nprivate function calculateTotal($order, $discount, $tax): float {\n    return $order->subtotal - $discount + $tax;\n}",
    "example_note": "Refactoring reduces assignments and conditions per method, lowering ABC while improving readability and testability.",
    "quick_fix": "Keep ABC < 20 per method; reduce assignments and condition chains via extraction and delegation.",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-27",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/abc_metric",
        "html_url": "https://codeclaritylab.com/glossary/abc_metric",
        "json_url": "https://codeclaritylab.com/glossary/abc_metric.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": "[ABC Metric (Assignments, Branches, Conditions)](https://codeclaritylab.com/glossary/abc_metric) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/abc_metric"
            }
        }
    }
}