← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Temporary Field

Code Quality Intermediate
debt(d7/e5/b5/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list phpstan and phpmd, but the automated flag is explicitly 'no' — these tools may flag null properties but cannot reliably detect the semantic pattern of a field only valid in one code path. The code_pattern description ('only set in one method and null everywhere else') requires contextual human judgment to identify, making this a code-review-level finding rather than a reliable automated catch.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix calls for moving fields into dedicated method parameters or a state-specific object. This is more than a one-line swap: it requires identifying all usages of the temporary field across the class and callers, creating or repurposing a value object or parameter, and updating call sites. It's a meaningful refactor within one or a few components, not a simple line-level replacement.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). Temporary fields apply across web, cli, and queue-worker contexts. Every maintainer who touches the class must reason about when the field is valid, adding cognitive overhead to reads, writes, and tests. The common mistake of adding null checks everywhere instead of eliminating the pattern compounds the burden over time. It doesn't define the system's shape, but it persistently slows down work on the affected class and its consumers.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception field explicitly states that developers treat a sometimes-null field as 'just an implementation detail,' failing to recognise it signals a missing class. The common mistakes confirm that null-initialisation actively hides the pattern, and that developers typically respond by adding null checks (making it worse) rather than eliminating the temporary nature. This contradicts standard OOP expectations that an object's fields are always in a valid, meaningful state.

About DEBT scoring →

Also Known As

temporary field smell sometimes-null field context-dependent field

TL;DR

A class field that is only set and used in certain circumstances — most of the time it is empty or null, confusing readers.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
// Field only meaningful during one operation — confuses readers
class OrderProcessor {
    private ?array $currentItems = null; // only set during process()
    private ?float $subtotal     = null; // only meaningful mid-calculation

    public function process(Order $order): void {
        $this->currentItems = $order->items;
        $this->subtotal     = $this->calculateSubtotal();
        // ...
        $this->currentItems = null; // reset when done
    }
}
✓ Fixed
// Move temporary fields to local variables or a parameter object
class OrderProcessor {
    public function process(Order $order): Invoice {
        $items    = $order->items;               // local — scope is clear
        $subtotal = $this->calculateSubtotal($items);
        return $this->buildInvoice($items, $subtotal);
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 106
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 2 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 16 Ahrefs 8 PetalBot 8 ChatGPT 7 SEMrush 7 Unknown AI 5 Google 5 Scrapy 5 Perplexity 2 Claude 2 Twitter/X 2 Applebot 2 Bing 2 Brave Search 2
crawler 65 crawler_json 7 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ 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
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Object property only set in one method and null everywhere else; field only meaningful during algorithm execution stored on object
Auto-detectable: ✗ No phpstan phpmd
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Class Tests: Update


✓ schema.org compliant