What is Law of Demeter (LoD)?
The Law of Demeter (LoD) aimed at reducing coupling in your code. It can be summed up as:
“Only talk to your immediate friends, not to strangers.”
A class or module should only use the things it directly depends on, not other objects those depend on. This keeps the code simpler, easier to test, and less connected to unnecessary details.
Code Example
Not recommended
<span>// Nested calls exposing internal structure</span><span>customerCity</span> <span>:=</span> <span>order</span><span>.</span><span>GetCustomer</span><span>()</span><span>.</span><span>GetAddress</span><span>()</span><span>.</span><span>GetCity</span><span>()</span><span>fmt</span><span>.</span><span>Printf</span><span>(</span><span>"Customer lives in: %s</span><span>\n</span><span>"</span><span>,</span> <span>customerCity</span><span>)</span><span>// Nested calls exposing internal structure</span> <span>customerCity</span> <span>:=</span> <span>order</span><span>.</span><span>GetCustomer</span><span>()</span><span>.</span><span>GetAddress</span><span>()</span><span>.</span><span>GetCity</span><span>()</span> <span>fmt</span><span>.</span><span>Printf</span><span>(</span><span>"Customer lives in: %s</span><span>\n</span><span>"</span><span>,</span> <span>customerCity</span><span>)</span>// Nested calls exposing internal structure customerCity := order.GetCustomer().GetAddress().GetCity() fmt.Printf("Customer lives in: %s\n", customerCity)
Enter fullscreen mode Exit fullscreen mode
Problem: The code relies on deeply nested calls, making it tightly coupled to the internal structure of Order
, Customer
, and Address
.
Better
<span>// Single method call hides internal structure</span><span>customerCity</span> <span>:=</span> <span>order</span><span>.</span><span>GetCustomerCity</span><span>()</span><span>fmt</span><span>.</span><span>Printf</span><span>(</span><span>"Customer lives in: %s</span><span>\n</span><span>"</span><span>,</span> <span>customerCity</span><span>)</span><span>// Single method call hides internal structure</span> <span>customerCity</span> <span>:=</span> <span>order</span><span>.</span><span>GetCustomerCity</span><span>()</span> <span>fmt</span><span>.</span><span>Printf</span><span>(</span><span>"Customer lives in: %s</span><span>\n</span><span>"</span><span>,</span> <span>customerCity</span><span>)</span>// Single method call hides internal structure customerCity := order.GetCustomerCity() fmt.Printf("Customer lives in: %s\n", customerCity)
Enter fullscreen mode Exit fullscreen mode
Improvement: The GetCustomerCity()
method encapsulates the internal details, exposing only the necessary functionality to the caller and reducing coupling.
Benefits of LoD
Reduced Coupling: Minimizes coupling between classes, making changes easier
Improved Readability: Code is more intuitive, focusing on high-level interactions
Hidden Implementation: Only relevant information is exposed, protecting the system from unnecessary complexity
Easier testing: Reduces the need for mocking complex nested calls
Applying LoD
Use DTOs (Data Transfer Objects) to limit the spread of data.
Apply Facade Patterns to simplify interactions with complex subsystems.
Refactor Chained Calls into single-level methods for clarity and adherence.
Others
Interested? Check out other posts from my programming principles series!
- Dependency Inversion Principle (DIP) Explained in 100 Seconds
- Golang Dependency Injection – Just in 5 Minutes!
- Interface Segregation Principle (ISP) Explained in 100 Seconds
- You Aren’t Gonna Need It Principle (YAGNI) Explained in 100 Seconds
- Liskov Substitution Principle (LSP) Explained in 100 Seconds
- KISS Design Principle Explained in 100 Seconds
- DRY Principle Explained in 100 Seconds
- “Tell, Don’t Ask” Principle Explained in 100 Seconds
Follow me to stay updated with my future posts:
暂无评论内容