{
    "slug": "replace_magic_literal",
    "term": "Replace Magic Literal with Symbolic Constant",
    "category": "quality",
    "difficulty": "beginner",
    "short": "Magic literals (numbers/strings hardcoded without context) should become named constants — MAX_RETRIES = 3 is self-documenting; the literal 3 is not.",
    "long": "Magic literals are unexplained values scattered through code. Types: magic numbers (if ($score > 42)), magic strings (if ($status === 'pnd')). Replace with: class constants (public const MAX_SCORE = 100), enums (Status::Pending), named constants (define('MAX_RETRIES', 3)), or config values. Benefits: single definition, searchable, self-documenting, single point to change. PHP: prefer class constants over global constants for scoping. Only exception: 0, 1, -1, true, false, empty string — universally understood.",
    "aliases": [],
    "tags": [
        "quality",
        "readability",
        "constants",
        "clean-code"
    ],
    "misconception": "Any named constant is better than a literal — constants for truly obvious values (0, 1, true) add noise without clarity.",
    "why_it_matters": "Magic literals make code impossible to understand without external documentation and create bugs when the same value is hardcoded in multiple places and one is missed during a change.",
    "common_mistakes": [
        "Using string literals for status values — use enums or class constants.",
        "Duplicating the same magic number in 10 places — one constant, 10 references.",
        "Creating constants with useless names: CONST_42 = 42."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "magic_number",
        "enums",
        "cyclomatic_reduction",
        "boolean_flags_refactor"
    ],
    "prerequisites": [
        "enums"
    ],
    "refs": [
        "https://refactoring.guru/replace-magic-number-with-symbolic-constant"
    ],
    "bad_code": "if ($attempts > 3) { sleep(30); } // What are 3 and 30?\nif ($status === 'pnd') { } // What is 'pnd'?",
    "good_code": "const MAX_RETRY_ATTEMPTS = 3;\nconst RETRY_DELAY_SECONDS = 30;\n\nif ($attempts > MAX_RETRY_ATTEMPTS) { sleep(RETRY_DELAY_SECONDS); }\n\nenum OrderStatus: string {\n    case Pending = 'pending';\n    case Complete = 'complete';\n}",
    "quick_fix": "Replace any hardcoded number/string with a named class constant or enum. Group related constants in an enum or dedicated constants class.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/replace_magic_literal",
        "html_url": "https://codeclaritylab.com/glossary/replace_magic_literal",
        "json_url": "https://codeclaritylab.com/glossary/replace_magic_literal.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": "[Replace Magic Literal with Symbolic Constant](https://codeclaritylab.com/glossary/replace_magic_literal) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/replace_magic_literal"
            }
        }
    }
}