Skip to main content
Object Calisthenics 70 XP · 7 min

One Dot per Line

Based on the Law of Demeter — don't reach through an object's internals. Ask the object to do the work instead of navigating its parts.

Showing
Ad (728×90)

Why this matters

Every dot after the first in a chain like order.customer.address.city is a hidden coupling. Your code now knows about Order, Customer, Address, and City — four classes instead of one. Rename any field in any of those classes and this single line breaks. The Law of Demeter says: talk only to your immediate neighbors. Don't navigate through objects to reach what you need — ask the closest object to do the work for you.

Code Challenge

Study the messy code, try to refactor it, then reveal the clean version.

Key takeaway

Count the dots. Each dot after the first is a dependency on something you shouldn't know exists. Move the logic into the object that owns the data — one dot per line keeps coupling local and refactoring safe.

Done with this lesson?

Mark it complete to earn XP and track your progress.