All laws

54Maintainability

Law of Demeter

Principle of Least Knowledge.

If

An object that reaches through other objects' internals (`a.getB().getC().doThing()`) couples itself to the entire dependency chain, so a change anywhere along that chain ripples back and breaks it.

Then

Only call methods on your direct collaborators, your arguments, objects you create, and your own fields. If you are chaining getters, add a method to the object that owns the data.

Unless

Pragmatism over Dogma — strict application breeds forwarding and wrapper bloat

Improve this article on GitHub