Low Coupling and High Cohesion in Software Design

Connect with

Oracle Java
In the previous article, I tried to describe about Cohesion and SRP ( Single Responsibility Principle), now I pick importance of High Cohesion and Low coupling in the software engineering specially while designing a component of the software.

As we know , software design is a creative process and it’s art rather than science, just like designing anything else. In analysis of software design many factors has to consider in which these two are important things. As per my understanding best design is flexible one not perfect one. Why I said this, because software functionalities keeps evolving according to time. We know that today’s best practices can be the tomorrow anti pattern/worst practice. I assume that today we design a component by considering today’s requirements and tried to carry forward today’s SLA (Service Level Agreement). If our design is flexible one so that tomorrow we can extend those else very difficult to refactor legacy code.

Low Coupling

“The degree of interdependence between two modules”

We aim to minimize coupling to make modules as independent as possible. ​How can we achieve low coupling can be achieve by: eliminating unnecessary relationships, reducing the number of necessary relationships and easing the tightness of necessary relationships.

There are two type of coupling:

  • Afferent coupling: Afferent means incoming. A class afferent couplings is a measure of how many other classes use the specific class.
  • Efferent Coupling: efferent means outgoing. A class efferent couplings is a measure of how many different classes are used by the specific class.
class Foo {
    Cool c;
}

class Bar {
    Cool c;
}

class Cool {
    // ...
}

Here, Foo and Bar each have one efferent coupling, and Cool has two afferent couplings.

At a Glance:

  • Coupling means– one object depending on other object
  • Two elements are coupled, if
    • One element has association (aggregation/composition) with another element.
    • One element implements/extends other element.

High Cohesion

Cohesion means “Stick together” as per dictionary, but in software engineering in designing a component, it’s a degree to focus a responsibility to a class. “There should be one and only one reason to change the class” this is the SRP (Single Responsibility Principle) but what about Cohesion. Cohesion and Single Responsibility Principle (SRP) goes hands-in-hands.

Summary

  • Try to adhere to the “high cohesion and low coupling” guideline on all levels of your code base.
  • Cohesion represents the degree to which a part of a code base forms a logically single, atomic unit.
  • Coupling represents the degree to which a single unit is independent from others.
  • Classes that adhere to the principle tend to have High Cohesion.
  • It’s impossible to achieve full decoupling without damaging cohesion, and vise versa.

References


Connect with

Leave a Reply

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