06
Encapsulate Variable
Goal
All reads and writes pass through a small named function that owns validation, logging, and invariants.
Before the refactoring
let defaultOwner = { firstName: 'Martin', lastName: 'Fowler' };After the refactoringlet _defaultOwner = { firstName: 'Martin', lastName: 'Fowler' };
function defaultOwner() { return _defaultOwner; }
function setDefaultOwner(o) { _defaultOwner = o; }Savings
A bug fix or audit becomes a one-line addition inside the wrapper; consumers never need to change.
Note
Adds a layer of indirection that pays off only when every access goes through the wrapper — leakage of direct access undoes the benefit.