intermediate

Law of Demeter

Limit knowledge of collaborator internals so code asks nearby objects for behavior instead of navigating deep object graphs.

Law of Demeter (principle of least knowledge): an object should talk to its immediate collaborators, not navigate deep graphs—avoid `order.customer.address.zip`. Ask `order.getShippingZip()` instead so internal structure can evolve.

Reduces coupling and ripple effects when intermediate objects change.

On interviews: rewrite a train wreck accessor chain into a meaningful domain method.

Common pitfalls: Demeter taken to absurd wrapper explosion, breaking legitimate fluent APIs, and DTO mapping that still leaks graph knowledge upward.

The trade-off is extra facade methods versus freedom to refactor object graphs.

Checklist:

  • No long accessor chains across objects.
  • Add behavior on the object that owns the data.
  • Balance with readable APIs—not one-line wrappers everywhere.