Concept · Core

Law of Demeter

Only talk to your immediate friends. A method should call methods on itself, its parameters, objects it creates, and its direct fields — not reach through a chain of strangers.

Asked atAmazonGoogleShopify
step 1 / 8
1/2The train wreck
getCustomer()customergetWallet()walletdeduct(amt)
Checkout
Order
Customer
Wallet
The train wreck
1order.getCustomer().getWallet().deduct(amt); // reaching through strangers
State
knows aboutOrder only (so far)

Three lifelines hang off Checkout. Checkout should only know its immediate friend Order — yet watch how far this one line of code actually reaches.