Design Patterns · Middle · Question 21 of 75
What does the Law of Demeter say about order.customer.address.city.name?
- ✓Correct answer: The code knows about objects that are too far away - talk to your neighbors
- 2The chain is fine if all the objects are created in one module
- 3It's a violation only when using optional chaining
?.
Explanation
"Talk only to your friends": long chains tie code to the internals of many objects, and a change in any of them breaks the caller. The fix is a method or selector like getDeliveryCity(order).