Law of Demeter: A Practical Guide to Loose Coupling - Kris Jusiak - CppCon 2021

Learn how to reduce complexity and maintain loose coupling in software systems with the Law of Demeter principle. Discover practical tips on dependency injection, abstraction, and SOLID principles to create maintainable and extendable code.

Key takeaways
  • The Law of Demeter principle is not about avoiding tight coupling but about reducing complexity and maintaining loose coupling in software systems.
  • Dependency injection is a way to improve decoupling and maintainability by giving objects the freedom to change their dependencies.
  • The speaker emphasizes that test-driven development and behavior-driven development can guide us in building the right code, and emphasizes the importance of applying SOLID principles for good design.
  • Tightly coupled code is hard to test and maintain, as it’s difficult to predict how one part of the system will affect the whole.
  • The speaker emphasizes the importance of injection and abstraction in loose coupling design to reduce complexity.
  • Injecting dependencies and using type erasure to decouple objects can make them more flexible and adaptable.
  • The speaker stresses that dependency inversion principle is important for testing and maintenance, as it makes it easy to swap out dependencies without affecting the functionality of the system.
  • Loosely coupled code is easier to maintain and extend over time, as each part can evolve independently without affecting the entire system.
  • To achieve loose coupling, we can separate concerns, encapsulate dependent components, and reduce coupling with interfaces and abstraction.
  • The Law of Demeter principle and dependency inversion principle are important design principles that should be applied to achieve good design and avoid tight coupling.
  • Tightly coupled code can lead to a range of problems, including maintenance difficulties and premature optimization.
  • Good design, including loose coupling, is achieved by applying SOLID principles, ensuring that each component has one responsibility, and using interfaces to decouple.
  • The speaker stresses that dependency injection doesn’t imply using a library or framework, but rather a way to decouple objects and make them more flexible and adaptable.
  • The importance of abstraction is emphasized, as it helps to decouple dependencies and make the system more maintainable.
  • Weak API design and bad naming can lead to hard-to-debug issues and maintenance nightmares.
  • By applying the Law of Demeter principle, software systems can be made more maintainable and easier to extend over time.
  • The importance of testing and verification in ensuring the reliability and maintainability of software systems is emphasized throughout the talk.