Object-Orientation AbusersMedium

Temporary Field: What It Costs and How to Fix It

Instance fields that are only set and used in specific circumstances, remaining empty or null the rest of the time.

Annual Cost$1.5k - $11k
Severity
3/5
CategoryObject-Orientation Abusers
Detection4 tools

What It Is

A Temporary Field is an instance variable that only has a meaningful value under certain conditions. For the rest of the object's lifecycle, the field is null, zero, or an empty sentinel value. Developers reading the class must figure out when the field is valid and when it is not, creating an invisible invariant that the type system does not enforce. Temporary Fields often arise when a complex algorithm needs workspace variables, and the developer stores them as fields rather than passing them as parameters.

Threshold: Any field that is null or meaningless for more than half the object's lifecycle is a temporary field. Fields set in one method and read in a different, non-sequential method are strong indicators.

Why It Costs Money

1

Null pointer exceptions and undefined behaviour. Code that accesses a temporary field outside its valid context gets null or zero, leading to bugs that only manifest in specific execution paths.

2

The class interface is misleading. Fields suggest the object always has that data. Developers write code that reads the field without realising it may be empty, and the bug passes code review because the field looks legitimate.

3

Testing requires understanding the lifecycle. To test code that uses the temporary field, you must know exactly which methods populate it and in what order, increasing test complexity.

Specific Cost Mechanisms

  • Null-access bugs: each incident costs 2-4 hours to diagnose because the null field looks valid from the class definition
  • Lifecycle confusion: developers waste 30-60 minutes understanding when each field is valid during code review
  • Test fragility: tests that depend on field population order break when the class is refactored

Estimated Annual Cost

Cost per instance by team size and codebase size. Based on $120,000 average developer salary. See full methodology.

Team SizeSmall (<50k LOC)Medium (50k-200k)Large (200k+)
3 devs$1,500$3,300$6,600
5 devs$2,500$5,500$11,000
10 devs$3,750$8,250$11,000
20 devs$5,000$11,000$11,000

How to Detect It

Specific rules and thresholds for automated detection. See full tool comparison.

SonarQube
squid:S1165 / squid:S1450

Fields assigned in one method and read in another unrelated method

CodeClimate
cognitive-complexity

Null-check branches around temporary fields inflate complexity

PMD
SingularField / UnusedPrivateField

Fields used in only one method path

IntelliJ IDEA
Field can be local variable

Directly detects fields that should be locals or parameters

Refactoring Patterns

Proven techniques to eliminate this smell. See all refactoring patterns.

Extract Class

The temporary fields form a coherent group used by a specific algorithm

Effort: 2-4 hours
Impact: Moves the fields to a class where they are always valid

Replace Method with Method Object

A complex method uses temporary fields as workspace

Effort: 3-6 hours
Impact: Encapsulates the algorithm with its workspace in a dedicated object

Introduce Null Object

The temporary field has a meaningful null/empty behaviour

Effort: 1-2 hours
Impact: Eliminates null-checking branches

Related Smells