Law of Demeter 00:00:00 Make a prediction You will locate a dependency on another object's internal structure and replace it with one useful request. Checkout reaches through customer, profile, address book and primary address to get a postcode. If the address book changes layout, which caller now breaks? Checkout can break despite needing only a postcode. Move the traversal behind the owner or give checkout a shipping-details value with the data it actually needs. 00:00:29 Paying for a pear Buying a pear, you hand the cashier a payment. You do not ask to open the cashier's purse, pick out a key and operate the shop's till yourself. Ask a collaborator for the outcome it knows how to produce. Reaching through its acquaintances couples you to arrangements it should be free to change. The Law of Demeter (LoD) is a rule about who a method may talk to. A method may call its own object, its parameters, the objects it creates, and the parts its own object holds. It should not call an object that it only got by asking one of those. order.customer.address.city looks like one line of data access. It is three promises: an order has a customer, a customer has exactly one address, an address has a city. When the shop starts selling gift orders with two addresses, every line with this chain is wrong, also in files nobody remembers. 00:01:28 Ask for the shipping postcode Checkout reaches through customer, profile, address book and primary address to read a postcode. Changing any intermediate representation breaks checkout. Ask the customer or a shipping details object for the postcode checkout actually needs. Keep the traversal inside the object that owns that knowledge. Add a customer with an address selected from several saved addresses. Checkout still asks the same business question. Move the address from a customer record to a shipping profile. A caller following each edge breaks even though the postcode it needs still exists. An operation asking the order for the destination can absorb that storage change in one owner. 00:02:16 A fluent chain can be fine A builder's set size, set colour and build calls return the same builder or its result under one documented API. Counting dots would wrongly condemn this fluent interface. Look for knowledge of strangers' internal structure instead. Configure size and colour through a fluent builder. Both calls operate on a documented construction surface. Compare that with fetching a customer, then an account, then a balance. Changing account ownership breaks the second chain; the number of dots fails to distinguish them. Counting dots would incorrectly reject a documented fluent builder that returns the same abstraction each time. The problem is inappropriate knowledge, not punctuation. A read-only value deliberately designed to expose navigation data can supply its fields directly without a chain of forwarding methods. A static checker can flag long navigation through unrelated objects. A framework can also generate a forwarding method for a property on a collaborator. Neither mechanism knows the business question by itself. Returning a shipping destination can hide storage details; blindly adding a forwarding method for every nested field simply exports those details under new names. 00:03:38 Try an interview follow-up How would you enforce it in a big code base? A type aware checker such as PMD, a baseline of current findings, and failing only on new ones. Exclude data packages. Rank the rest by how often the file changes and how many files repeat the same chain. A caller walks from order to customer to address to postcode just to ask whether delivery is supported. Move address storage into a separate shipping profile. Count the callers that now break. A delivery eligibility operation can own the lookup and let those callers ask the original business question directly. 00:04:19 What to take with you Friends are yourself, your parameters, what you create and your own parts. Anything else that a friend hands out is a stranger. Ask the friend for the result, not for the stranger. Counting dots misses the design issue. A fluent builder can return itself after each configuration step. In contrast, fetching the customer from an order into a local variable and then reaching into that customer still exposes the same chain. Plain data and fluent interfaces are no violation.