Shared mutable default
Everyone gets the same object, and one of them changes it.
What it looks like
- A default returned by reference rather than copied.
- A module-level object used as a fallback and then written to.
- A cache of 'empty' values that callers are free to mutate.
Why it survives review
The values are identical, so nothing looks wrong on the first call, the second, or in any test that reads without writing. The defect only appears once one caller mutates what it was given — at which point every other caller, including ones that ran earlier, sees the change.
How to see it
- For any default or fallback, ask whether the caller receives a fresh value or a shared one.
- A
constat module scope holding an object or array is shared by everything that touches it. - Look for the write. A shared default is harmless until somebody mutates it, so find out whether anybody does.
A minimal pair
Correct
export function defaults() {
return { ...BASE };
}
Defective
export function defaults() {
return BASE;
}
Two callers, one of which sets a field — and the second caller's 'defaults' now carry the first one's change.
Practise it
No exercise in the corpus sets this class yet. The lesson stands on its own — the corpus grows by adding subjects, and pretending otherwise would hide the gap.