Design Principles

Law of Demeter

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

design coupling abstraction

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.

Enforce this law (and 100+ more) in your AI coding agents with DevLaws.

Open DevLaws All laws →