Design Principles

Law of Demeter

A module should only talk to its immediate friends, not to strangers.

Also known as the Principle of Least Knowledge, the Law of Demeter states that an object should only call methods on:

  1. Itself
  2. Its parameters
  3. Objects it creates
  4. 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.

Resources

Open the interactive DevLaws app for protocols, bookmarks, and AI enforcement.

Open in app Browse all laws Marketing site