Also known as the Principle of Least Knowledge, the Law of Demeter states that an object should only call methods on:
- Itself
- Its parameters
- Objects it creates
- Its direct component objects
Bad (violates Demeter)
user.getAddress().getCity().getZipCode();
Good (respects Demeter)
user.getZipCode(); // Encapsulates the chain internally
This reduces coupling and makes code easier to refactor.