{
    "slug": "cyclomatic_reduction",
    "term": "Reducing Cyclomatic Complexity Techniques",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "Cyclomatic complexity counts independent code paths — reduce it by early returns, extracting conditions to named functions, using lookup tables, and replacing switch/if chains with polymorphism.",
    "long": "Cyclomatic complexity (CC) = branches + 1. CC > 10 is complex, > 20 is very complex and untestable. Techniques to reduce: (1) Early returns / guard clauses eliminate nesting, (2) Extract complex conditions to named predicate functions, (3) Replace switch/if chains with lookup tables or strategy pattern, (4) Decompose large functions into smaller ones, (5) Use polymorphism instead of type-checking conditions, (6) Replace nested ternaries with match/switch or named functions. Tools: PHPStan, PHP_CodeSniffer (PHPMD), ESLint (complexity rule), SonarQube.",
    "aliases": [],
    "tags": [
        "quality",
        "complexity",
        "refactoring",
        "clean-code"
    ],
    "misconception": "Low cyclomatic complexity always means simple code — a function with 3 levels of nested callbacks can be complex to understand despite low CC.",
    "why_it_matters": "High cyclomatic complexity means more paths to test and more places bugs can hide — functions with CC > 10 have exponentially more edge cases.",
    "common_mistakes": [
        "Ignoring CC metrics in code review.",
        "Reducing CC by moving complexity into long boolean expressions.",
        "Not using early returns — unnecessary else after return increases nesting."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "time_complexity",
        "extract_function",
        "postmortem",
        "guard_clause"
    ],
    "prerequisites": [
        "extract_method",
        "guard_clauses"
    ],
    "refs": [
        "https://phpmd.org/rules/complexity.html"
    ],
    "bad_code": "function process($user, $action) {\n    if ($user) {\n        if ($user->isActive()) {\n            if ($action === 'delete') {\n                if ($user->isAdmin()) {\n                    // do delete\n                } else {\n                    throw new Exception('No permission');\n                }\n            }\n        } else {\n            throw new Exception('Inactive');\n        }\n    } else {\n        throw new Exception('No user');\n    }\n}",
    "good_code": "function process($user, $action) {\n    if (!$user) throw new Exception('No user');\n    if (!$user->isActive()) throw new Exception('Inactive');\n    if ($action === 'delete') deleteUser($user);\n}\n\nfunction deleteUser($user) {\n    if (!$user->isAdmin()) throw new Exception('No permission');\n    // do delete\n}",
    "quick_fix": "Add early returns for guard conditions. Extract complex conditions to named boolean functions. Decompose functions over CC 10 into smaller ones.",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/cyclomatic_reduction",
        "html_url": "https://codeclaritylab.com/glossary/cyclomatic_reduction",
        "json_url": "https://codeclaritylab.com/glossary/cyclomatic_reduction.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": "[Reducing Cyclomatic Complexity Techniques](https://codeclaritylab.com/glossary/cyclomatic_reduction) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/cyclomatic_reduction"
            }
        }
    }
}