A module-level variable or shared mutable state read and written directly from any consumer — no central audit point for changes.
The agent finds a variable read and written from multiple consumers with no central function owning the access; reasoning about any read requires modeling every writer.
All reads and writes pass through a small named function that owns validation, logging, and invariants.
All access goes through a small named function the agent can grep for, audit, and instrument as a single closed surface.
Before the refactoring
let defaultOwner = { firstName: 'Martin', lastName: 'Fowler' };
After the refactoring
let _defaultOwner = { firstName: 'Martin', lastName: 'Fowler' };function defaultOwner() { return _defaultOwner; }function setDefaultOwner(o) { _defaultOwner = o; }
Adding validation, logging, or invariant checks requires touching every consumer; bugs from concurrent mutation hide in the gaps; the variable's lifetime is implicit.
Any change to validation, logging, or invariants requires the agent to update every consumer; concurrent edits compound the search-and-update cost.
Adds a layer of indirection that pays off only when every access goes through the wrapper — leakage of direct access undoes the benefit.
Indirection at every call site adds a hop; if any consumer leaks past the wrapper, the encapsulation's safety promise silently breaks and the agent assumes guarantees that don't hold.
A bug fix or audit becomes a one-line addition inside the wrapper; consumers never need to change.
The agent has one audit point for validation/logging/invariants; consumers don't need to change when the wrapper grows new behavior.
Adding a getter/setter wrapper without removing the direct access — adds indirection ceremony while consumers still bypass the wrapper, so the encapsulation provides no real guarantee.
Wrapping the variable without enforcing wrapper-only access leaves leaks the agent doesn't see; the wrapper becomes a false safety signal.