Temporary Field
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);
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
20
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
Amazonbot 10
Google 4
Unknown AI 3
Perplexity 2
ChatGPT 2
Ahrefs 1
Also referenced
How they use it
crawler 17
crawler_json 4
pre-tracking 1
Related categories
⚡
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