57

Change Value to Reference


Goal

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

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);
Savings

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

Note

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