Law of Demeter (LoD) Explained in 100 Seconds

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!


Follow me to stay updated with my future posts:

原文链接:Law of Demeter (LoD) Explained in 100 Seconds

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
There's only one corner of the universe you can be sure of improving, and that's your own self.
这个宇宙中只有一个角落你肯定可以改进,那就是你自己
评论 抢沙发

请登录后发表评论

    暂无评论内容