DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more

Quality Code Is Loosely Coupled

Agile is all about flexibility and speed of development. Loosely coupled code is all about flexibility and ease of change. It's a match made in heaven.

David Bernstein user avatar by
David Bernstein
CORE ·
May. 12, 17 · Opinion
Like (15)
Save
Tweet
Share
8.36K Views

Join the DZone community and get the full member experience.

Join For Free

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# 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.

Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mission-Critical Cloud Modernization: Managing Coexistence With One-Way Data Sync
  • Automated Testing With Jasmine Framework and Selenium
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Unlock the Power of Terragrunt’s Hierarchy

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: