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

Coupling and Cohesion: Confounding Yet Critical Concepts

Ideally, you want all your software to have low coupling and high cohesion. But only the Sith deal in absolutes.

Grant Schultz user avatar by
Grant Schultz
·
Feb. 21, 19 · Opinion
Like (2)
Save
Tweet
Share
7.81K Views

Join the DZone community and get the full member experience.

Join For Free

A deep but often-overlooked problem that perennially plagues software is that of high coupling and/or low cohesion. The underlying concepts are foundational to good software engineering, as they impact how hard it will be to comprehend, extend, and maintain a particular piece of software. Yet they can be quite confusing, and attempts to deal with them can be very misguided. Because a shallow understanding can do more harm than no understanding at all, the subtleties of these related concepts need to be explored.

To review very briefly, coupling is the degree to which distinct classes, modules, etc., are tied together, such that a change to one requires changes to others. Cohesion is the degree to which a class or component deals with just one thing. But rather than thinking of cohesion as tight internal coupling, think of it as the conceptual purity of a unit of software. This avoids some misunderstandings that can arise otherwise. I won't dwell further on the definitions, as others have done a good job of that.

Of course, the goal is to have low coupling and high cohesion, as this seems to give the most understandable and maintainable code structure. It can seem as if they are polar opposites, because an increase in software entropy is likely accompanied by both increasing coupling and decreasing cohesiveness. But they are not diametrically opposing characteristics, as it is possible (although less likely) to have high coupling and high cohesion at the same time.

Some coupling metrics only look at software units in a pairwise fashion. But coupling can be such that a change in one place requires changes in multiple places in of code—in order to maintain consistency of naming, or to pass new parameters across process boundaries, just to name a couple of examples. (Although I disagree with this post's conclusion, the author's explanation of coupling's ripple-effect nature is worth reading.) If you could measure the strength of coupling, combined with the distance over which every instance every possible change would propagate, you might have the beginnings of a useful coupling metric. Of course, this is not feasible, and coupling has to remain somewhat subjective, rather than rigorously measurable.

Since not every change to code is equally likely, another factor that should ideally be taken into account is the probability of potential changes getting made to an application. Tightly-coupled code that is rarely touched doesn't cost that much extra. But as above, measuring this is not practical.

Metrics can be misued in many ways, one of the worst being that of comparing different developers' work. This is a misuse, because unless two developers write solutions to the exact same problem, their coupling/cohesion metrics are not comparable. Even if you had a way to normalize metrics for application size (and please tell me you would not just divide by lines-of-code), different problems have different amounts of essential complexity. This, in turn, has to affect the coupling in an application. Therefore, to judge someone harshly because their application is more highly coupled is short-sighted, to say the least.

Artificial attempts to reduce coupling can also be misguided. Dependency Injection (DI) is a popular technique that supposedly reduces coupling. But if you diagram an application before and after DI—being sure to include every real link, you will see that the amount of coupling is actually higher with dependency injection. (See one list of DI's disadvantages.) This a case where a shallow but "cool" solution to a deep problem actually makes things worse.

Even refactoring a class into several smaller classes can be a bad idea if the total amount of coupling (or even the amount of code) in the system has to increase. (After all, it's still solving the same problem as before you refactored.) Research into coupling has shown that even well-structured applications seem to naturally contain a small number of modules that have to be highly coupled. This suggests that we should not expend great effort to blindly follow simple rules, while ignoring what those rules are forcing us to do to our code.

If software has timeless problems, we should expect the solutions to be equally timeless. Software engineering would be better served if we kept our minds on deeper principles like coupling and cohesion, instead of the latest trendy language, framework, or development fad. We should focus our energy on structuring and then growing software in ways that actually have a large and long-term positive impact on its cost to build and maintain.

Cohesion (computer science) Concept (generic programming) Software engineering application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Kubernetes vs Docker: Differences Explained
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer

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: