• Solutions
    • I Can Help
      • Define
      • Educate
      • Integrate
      • Advise
    • Consultation
      • Assessments
      • Design Reviews
      • Code Inspections
      • Ongoing Support
    • Training Classes
      • Training Schedule
      • On-Site Classes
      • Online Training
      • Customized Coaching and Mob Facilitation
    • Learning Roadmap
      • Developer Essentials Training
        • Agile Analysis and Design Patterns
        • Hands-On: Extreme Programming Practices
      • Scrum Framework Developer Essentials
      • Design Pattern Developer Essentials
      • Object-Oriented Analysis and Design Patterns
      • Scrum Software Developer Essentials
      • Agile Software Developer Essentials
      • Agile Software Developer Intensives
    • Speaking
      • Beyond the Legacy Code Crisis
      • Five Developer Practices for Agile Software
      • Writing High Quality, CLEAN Code
      • Essential Scrum Developer Practices
      • Improving Your Scrum Process
      • The Agile Development Advantage
    • Testimonials
    • Clients
  • Resources
    • Blog
    • My Book: Beyond Legacy Code
    • Bibliography
    • Referral Program
    • Associates
  • Contact
    • Contact Me
    • Schedule A Call
    • About
  • Schedule A Call

Quality Code is Loosely Coupled

  1. Home
  2. Blog
  3. Quality Code is Loosely Coupled

2025 Public Training Schedule

June 23 26, 2025 – Agile Analysis and Design Patterns – Half-Day Sessions Online

July 22 – 25, 2025 – Agile Analysis and Design Patterns – Half-Day Sessions Online

Register Now
Or schedule a private class
Course Descriptions
  • Developer Essentials Training
    • Agile Analysis and Design Patterns
    • Hands-On: Extreme Programming Practices
  • Object-Oriented Analysis and Design Patterns
  • Scrum Software Developer Essentials
  • Agile Software Developer Essentials
  • Agile Software Developer Intensives
Follow me on Twitter:

Follow @ToBeAgile

Blog Post Categories
  • Announcements {19}
  • Articles {2}
  • Bits and Pieces {74}
  • Blogosphere {1}
  • Bonuses {2}
  • Build in Small Batches {27}
  • Collaborate {38}
  • Create CLEAN Code {34}
  • Implement the Design Last {11}
  • Integrate Continuously {30}
  • Off-Topic but Interesting {3}
  • Rants {82}
  • Refactor Legacy Code {24}
  • Say What, Why, and for Whom Before How {22}
  • Seven Strategies {38}
  • Specify Behaviors with Tests {17}
  • Write the Test First {27}
Archives
  • 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • 2022
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • 2021
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • 2020
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • 2019
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • 2018
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • 2017
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • 2016
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • 2015
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • 2014
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • 2013
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • 2012
  • December 2012
  • Privacy Policy

    (c) 2025 To Be Agile

    • May 10, 2017 ( 2 comments )
    • Bits and Pieces

    While cohesion, which was the subject of the last three posts, is about the internal consistency of a class or method, coupling is a code quality that looks at the relationship between entities.

    There are two distinct forms of coupling: loose coupling and tight coupling. One is good, the other isn’t—and I always forget which one is which. They both sound pretty bad to me so I prefer using the terms intentional coupling and accidental coupling instead.

    The best kind of coupling is part of the domain model. We want the right kind of coupling in our systems. We’re not striving for systems with no coupling at all. A system with no coupling is a system that does nothing! Instead we’re striving for the right kind of coupling, one that’s needed to create the right kind of behavior but no more.

    I’ve found that bad coupling usually gets into a system as a secondary effect, the primary cause of which is that one of the other code qualities are missing.

    For example, when classes are not cohesive they have many reasons for clients to couple to them. This type of approach, which is a common practice, causes systems to be tightly bound together making it very difficult to change and to extend.

    Loose coupling is about making external calls indirectly through abstractions such as abstract classes or interfaces. This allows the code to run without having to have the real dependency present, making it more testable and more modular.

    One of the best known techniques for working with external dependencies is dependency injection. Dependency injection is simply the practice of separating the instantiation of a service from its usage. If you want to use a service, then instead of instantiating it and invoking it, request that another entity instantiate it and then give you access to it because then you can be passed a mock instead of the real object, which will help facilitate automated testing.

    It’s really that simple act of injecting dependencies rather than instantiating them that lets us automate the testing of our software.

    I want my coupling to be explicit. I want to keep the relationships between my entities clear and intentional, using strong contracts. This helps me avoid a whole range of errors and also nicely partitions my system.

    We want coupling to reflect the nature of the problem and if we model a system well then each class only knows about what it needs to know about. This is how we limit knowledge in a system. The reason we want to limit knowledge in a system is that it limits points of failure and reduces the impact of change on a system.

    Generally speaking, the more loosely coupled the system is, the more flexible it is. Sometimes we need maximum flexibility so we look at patterns like the Observer Pattern or the Vsistor Pattern. Other times we don’t need a lot of flexibility so we opt to build software in such a way that it reflects the structure of what it is we’re modelling.

    Coupling is all about the relationship between classes and we only get a handful of relationships that we can have between classes. One class can contain another class, which in languages like Java and C Sharp means that one object has reference to another object. These languages also have the notion of inheritance where one class can inherit from another class. Classes can also implement a common interface.

    These are the primary ways in which classes can relate to each other. There are only a handful, so using the right ones in the right ways is absolutely critical.

    Does a package have an address or does an address have a package? We don’t give these things a second thought in the real world, but in the virtual world we make the rules, so we have to be explicit.

    I want coupling where I intend it to be and no coupling where I don’t intend it to be. It’s that simple in theory, but in practice… well, that’s another story.

    {2 Comments }

    • Noah Sherrill says:
      May 12, 2017 at 2:36 pm

      I like your definition of dependency injection; it’s the simplest I have seen. This post is unusual also in that, when I have encountered discussions of coupling before, people generally talk of minimizing it. However, you make a very good point, if we absolutely minimize coupling to the point that we have no coupling at all, the software will no longer serve any purpose. The perspective of finding the right kind of coupling rather than minimizing it is useful.

      Reply
    • David Bernstein says:
      May 13, 2017 at 4:11 pm

      Thank you, Noah. I’m glad you’re enjoying my series on code qualities. Stay tuned as I have more to share about coupling and other code qualities.

      David.

      Reply

    Previous Post: « Cohesion and Testability

    Next Post: Pathologies of Tight Coupling »

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Solutions

    I Can Help
    • Define
    • Educate
    • Integrate
    • Advise

    Consultation
    • Assessments
    • Design Reviews
    • Code Inspections
    • Ongoing Support

    Testimonials Clients

    Resources

    • Blog
    • My Book: Beyond Legacy Code
    • Referral Program
    • Associates


    Read my book!
    Schedule a Call

    Training

    • Training Schedule
    • Learning Roadmap
    • On-Site Classes
    • Online Training
    • Customized Coaching and Mob Facilitation

    Course Descriptions
    • Developer Essentials Training
      • Agile Analysis and Design Patterns
      • Hands-On: Extreme Programming Practices
    • Object-Oriented Analysis and Design Patterns
    • Scrum Software Developer Essentials
    • Agile Software Developer Essentials
    • Agile Software Developer Intensives

    Copyright 2025 © To Be Agile. All rights reserved. Privacy Policy.

    This website uses cookies to improve your experience. I'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
    Privacy & Cookies Policy

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    SAVE & ACCEPT