Encapsulate Variable

Compare
Symptom
Human

A module-level variable or shared mutable state read and written directly from any consumer — no central audit point for changes.

Agent

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.

Goal
Human

All reads and writes pass through a small named function that owns validation, logging, and invariants.

Agent

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; }
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Adding validation, logging, or invariant checks requires touching every consumer; bugs from concurrent mutation hide in the gaps; the variable's lifetime is implicit.

Agent

Any change to validation, logging, or invariants requires the agent to update every consumer; concurrent edits compound the search-and-update cost.

Tradeoff
Human

Adds a layer of indirection that pays off only when every access goes through the wrapper — leakage of direct access undoes the benefit.

Agent

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.

Relief
Human

A bug fix or audit becomes a one-line addition inside the wrapper; consumers never need to change.

Agent

The agent has one audit point for validation/logging/invariants; consumers don't need to change when the wrapper grows new behavior.

Trap
Human

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.

Agent

Wrapping the variable without enforcing wrapper-only access leaves leaks the agent doesn't see; the wrapper becomes a false safety signal.