Comparison
EncapsulationvsLaw of Demeter
Encapsulation
another team reached into the field directly, and now you cannot rename it without breaking them.
Keeping a module's internals private so they stay changeable. The value is not secrecy — it is that whatever you expose becomes a contract you have to honour, and whatever you hide stays yours. Every leaked internal converts a future refactor from a local decision into a negotiation with everyone who reached in, which is the mechanism behind Hyrum's law.
Full entry →Law of Demeter
you wrote order.getCustomer().getAddress().getPostcode() and a change three objects away broke your line.
Talk only to your immediate collaborators, not to their collaborators' collaborators. Each dot in a chain like that is a structural assumption about somebody else's internals, so a chain of four is four ways for a change elsewhere to break you. The point is not the dot count — it is that you have coupled to a shape rather than to a service, and the fix is usually to ask the first object for the answer rather than for its parts.
Full entry →