Law of Demeter

Last updated

What is Law of Demeter?

Also called “Principle of Least Knowledge”, it states:

In Practice

  • Avoid chaining calls deep into the internals of other objects.
  • Restrict communication to objects you directly manage.

🔹 ❌ Bad Example (Violation)

// Controller
total = order.customer.address.getRegionTaxRate() * order.amount

🔹 ✅ Good Example

// Controller
total = order.calculateTotal()

🔹 Why It Matters

  • Reduces coupling → fewer dependencies between classes.
  • Increases maintainability → changes in one class don’t affect distant classes.
  • Improves readability → clear boundaries of responsibility.

🔹 Resources

Free resources to learn Law of Demeter

More in stage 4: Design Principles

Law of Demeter: common questions

Can I learn Law of Demeter in Hindi?

This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Law of Demeter in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.

Is this Law of Demeter tutorial for beginners?

It is lesson 34 of 94 in the free Software Design and Architecture roadmap, in stage 4, Design Principles. If you are new to Software Design and Architecture, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.

Still confused? Ask me