Ley de Demeter

Principio del mínimo conocimiento · No hables con extraños

Escrito por Darjus Kazakevic · revisado el

Talk to your friends, not to their friends.

The Law of Demeter, also called the Principle of Least Knowledge, is a rule about coupling: how much one piece of code depends on another. A method may call itself, its fields, its parameters and objects it creates. It should not reach through those to their collaborators.

The symptom is a chain such as order.getCustomer().getAddress().getZip(). The caller now depends on three classes and breaks when any of them changes. The fix is Tell, Don't Ask: ask Order for what you need, such as order.shippingZip(), and let Order find it.

En esencia — A method calls only its own object, its fields, its parameters and objects it creates, and never reaches through them to objects further away.

Cuándo recurrir a él

Recurre a él cuando

  • Objects with rules to protect, such as aggregates and entities
  • You see chains of getters that walk from object to object
  • You want to change a class's internals without breaking far callers

Reconsidéralo cuando

  • Fluent builders and pipelines that chain by design
  • DTOs, config objects and parsed JSON with no behaviour
  • When it would add many pass-through methods that mean nothing

Who you are allowed to call

Your direct friends
The objects a method may call: itself, its own fields, its parameters, and objects it creates inside the method. The method already depends on these, so calling them adds no new coupling.
The train wreck (message chain)
A chain of getters that walks from one object, through its collaborators, to something deep inside. Each extra step is a new dependency on a class the caller should not know.
Tell, Don't Ask
The usual fix. Instead of asking an object for its parts and doing the work yourself, tell the object to do the work. The walk moves inside the object that owns the data.
Data structures are exempt
The law protects objects that hold behaviour and rules. A plain data structure (a DTO, a config record, parsed JSON) has nothing to hide, so reading its nested fields is fine.

Reglas de diseño

Tell the object; do not walk its graph
Instead of reading parts and doing the work yourself, call a method on the object you hold. order.shippingLabel() keeps Customer and Address private to Order.
Judge coupling, not dots
builder.a().b().c() returns the same object each time, so it is fine. Reading fields of a DTO or parsed JSON is fine too. The target is reaching into objects that own behaviour.
Move behaviour, not getters
Replacing a.getB().getC() with a new a.getC() only hides the chain. Move the work itself to the object that owns the data, or the coupling is still there.

Errores habituales

  • Counting dots instead of thinking about coupling, and flagging fluent APIs as violations.
  • "Fixing" it with pass-through getters, which moves the chain one level in.
  • Applying it to DTOs and config objects that have nothing to hide.
  • Wrapping so much that every object becomes a thin forwarder with no behaviour of its own.