16
Temporary Field
Symptom
A class field used by only one method, set to null or default the rest of the time.
Goal
The temporary state moves to a dedicated class that exists only when it's relevant.
Smellier version
class Order {
shippingTrack = null;
ship() {
this.shippingTrack = computeTrack();
}
}Fresher versionclass Order { ship() { return new Shipment(this); } }
class Shipment { /* owns the track */ }Savings
Null-checks vanish; class invariants tighten; the new class has a clear purpose.
Note
Reader must trace the conditions under which the field is meaningful; null-checks scatter; the field's role is unclear.