{
    "slug": "temporary_field",
    "term": "Temporary Field",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "A class field that is only set and used in certain circumstances — most of the time it is empty or null, confusing readers.",
    "long": "Temporary Field is a smell where an instance variable is only populated during part of the object's lifecycle — set before a complex operation and ignored at all other times. Readers must understand when the field is valid and when it isn't, adding cognitive load. The refactoring is usually Extract Class: move the temporary fields and the methods that use them into a dedicated parameter object or method object, making the state explicit and scoped.",
    "aliases": [
        "temporary field smell",
        "sometimes-null field",
        "context-dependent field"
    ],
    "tags": [
        "code-smell",
        "oop",
        "refactoring"
    ],
    "misconception": "A field that is only populated in some code paths is just an implementation detail. Temporary fields confuse maintainers who cannot tell when a field is valid — they usually signal a missing class that captures the context where those fields are always populated.",
    "why_it_matters": "Temporary fields — set only in some code paths — make objects unpredictable; readers cannot know whether a field will be valid when they access it.",
    "common_mistakes": [
        "Setting instance properties only inside specific methods and using them elsewhere without null checks.",
        "Using $this->result as a temporary accumulator in a method instead of a local variable or return value.",
        "Not noticing temporary fields because they are null-initialised — the null hides the pattern.",
        "Fixing temporary fields by adding null checks everywhere rather than eliminating the temporary nature."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "code_smell",
        "single_responsibility",
        "value_object"
    ],
    "prerequisites": [
        "single_responsibility",
        "cohesion",
        "value_object"
    ],
    "refs": [
        "https://refactoring.guru/smells/temporary-field"
    ],
    "bad_code": "// Field only meaningful during one operation — confuses readers\nclass OrderProcessor {\n    private ?array \\$currentItems = null; // only set during process()\n    private ?float \\$subtotal     = null; // only meaningful mid-calculation\n\n    public function process(Order \\$order): void {\n        \\$this->currentItems = \\$order->items;\n        \\$this->subtotal     = \\$this->calculateSubtotal();\n        // ...\n        \\$this->currentItems = null; // reset when done\n    }\n}",
    "good_code": "// Move temporary fields to local variables or a parameter object\nclass OrderProcessor {\n    public function process(Order \\$order): Invoice {\n        \\$items    = \\$order->items;               // local — scope is clear\n        \\$subtotal = \\$this->calculateSubtotal(\\$items);\n        return \\$this->buildInvoice(\\$items, \\$subtotal);\n    }\n}",
    "quick_fix": "Move temporary fields that are only valid in some states into a dedicated method parameter or a state-specific object — a null field on a domain object is a sign it doesn't belong there",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/temporary_field",
        "html_url": "https://codeclaritylab.com/glossary/temporary_field",
        "json_url": "https://codeclaritylab.com/glossary/temporary_field.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": "[Temporary Field](https://codeclaritylab.com/glossary/temporary_field) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/temporary_field"
            }
        }
    }
}