DEV Community

Dzung Nguyen
Dzung Nguyen

Posted on

1

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.

LoD Image


Code Example

❌ Not recommended

// 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

// 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:

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay