Coupling and cohesion are methods to measure the relationship between and within modules.
A software system is divided into multiple modules, where each and every module are capable of performing a function independently. This technique is known as Modularization.
Further, Coupling and Cohesion indicate to which degree a module can carry out a task on its own and the strength of elements related to one other within that respective module.
Coupling measures interaction between modules whereas Cohesion focuses on interaction within a module.
Difference between Coupling and Cohesion in Tabular form
Coupling | Cohesion |
---|---|
It refers to relationship between two or more modules | It refers to relationship between two or more elements within a module |
It focuses on measuring a module's dependency with other modules | It focuses on measuring a module's functional strength |
It is ideal to have low coupled modules which states dependency between modules | It is ideal to have a module with high cohesiveness which states implementation of a particular feature with low or no communication with other module |
It is based on communication between two modules. Therefore, it is known as inter-module concept | It is based on communication between two elements or fields within a single module. Therefore, it is known as intra-module concept |
There are 6 types of coupling: Data Coupling,Stamp Coupling,Control Coupling,External Coupling,Common Coupling and Content Coupling | There are 8 types of cohesion: Functional Cohesion,Sequential Cohesion,Communicational Cohesion,Procedural Cohesion,Temporal Cohesion,Logical Cohesion and Coincidental Cohesion |
It is not possible to achieve no coupling among all modules | It is possible to achieve full cohesive modules |
Coupling
The measure of how a module depends on other modules is known as coupling.
It can be viewed as highly coupled, loosely coupled and uncoupled. These are based on their degree of dependencies.
Uncoupled signifies no dependencies at all, all the modules or methods have nothing to do with other modules. No communication between this type of coupling takes place.
Highly coupled refers to the high dependency of the module with each other.
Loosely coupled represents less dependency which means that only essential piece of code would depend and communicate with each other. It means undesired data would not be passed to another section of code.
In programming, the ideal way of having a program to be loosely coupled is by using the concept of interfaces.
Coupling can be classified based on certain characteristics:
Cohesion
It represents how a module’s attributes or fields are functionally related together. A module that is highly cohesive, has little or no interaction with other modules.
Therefore, it is better to have maximised cohesive modules where elements within a module interact properly to one another.
A module with high cohesion represents that it is strongly capable of performing a particular functionality or task, considering less or no communication with other modules.
Cohesion can be classified based on certain characteristics:
A Good Relationship between Cohesion and Coupling
A software with less coupled and high cohesive module design should always be preferred. A software design breaks down into multiple modules where each module solves a particular problem.
These modules are structured in a proper hierarchy. Each and every module must be implemented in a way that they are having less dependency with other modules and the elements within that module should be functionally related together.