Change Value to Reference

Removes smells
Compare
Symptom
Human

Duplicate copies of a logically-single entity scattered across the codebase — every order carries its own Customer copy that should be the shared customer.

Agent

Duplicate copies of a logically-single entity scattered across the codebase; the agent updating the entity must find and update every copy consistently.

Goal
Human

Duplicate copies of a logically-single entity collapse into one shared object that everyone references.

Agent

The entity exists once; the agent reasons about one canonical object referenced everywhere.

Before the refactoring

// every order carries its own Customer copy
orders.forEach(o => o.customer = { name: 'Acme' });

After the refactoring

const acme = customerRepository.find('Acme');
orders.forEach(o => o.customer = acme);
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Updates to the entity must touch every copy; storage bloats; identity becomes ambiguous (which copy is the canonical one?).

Agent

The agent must coordinate updates across every copy; identity becomes ambiguous and the agent can't tell which copy is canonical.

Tradeoff
Human

Sharing introduces the question 'who owns this?' — make sure the lifetime and visibility of the shared reference are well-defined.

Agent

Sharing references introduces lifetime and visibility ambiguities (who owns this? when does it get freed?) the agent must reason about; the original value-copies sidestepped this.

Relief
Human

Updates to the entity are visible everywhere; storage shrinks; identity becomes meaningful again.

Agent

Updates land in one place; storage shrinks; the agent reasons about the entity as a single referent with meaningful identity.

Trap
Human

Sharing references without clear ownership — introduces lifetime and visibility ambiguities (who owns this? when does it get freed?) that the original value-copies hid.

Agent

Sharing references without explicit ownership creates lifetime ambiguities the agent must model — the cure introduces a different category of bug than the original duplication.