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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Protect Your Invariants!
  • Single Responsibility Principle: The Most Important Rule in the Software World
  • Reference Architecture: Deploying WSO2 API Manager on Microsoft Azure
  • Apache Cassandra Horizontal Scalability for Java Applications [Book]

Trending

  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Scalability 101: How to Build, Measure, and Improve It
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink

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.

By 
Grant Schultz user avatar
Grant Schultz
·
Feb. 21, 19 · Opinion
Likes (2)
Comment
Save
Tweet
Share
9.0K 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.

Related

  • Protect Your Invariants!
  • Single Responsibility Principle: The Most Important Rule in the Software World
  • Reference Architecture: Deploying WSO2 API Manager on Microsoft Azure
  • Apache Cassandra Horizontal Scalability for Java Applications [Book]

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!