Cohesion and Coupling in Software Engineering
Cohesion and Coupling in Software Engineering
In software engineering, cohesion and coupling are important concepts that describe how
well different parts of a system work together. They impact the maintainability, scalability,
and quality of software. Here’s a breakdown of each concept:
Cohesion
Cohesion refers to how closely related the responsibilities of a single module or class are. It
measures the strength of relationships between the functionalities performed by that module.
High cohesion within a module implies that all the elements of the module work towards a
single, well-defined task.
1. Functional Cohesion:
o The module performs one specific task, and all its elements are essential for
performing that task. This is the ideal type of cohesion.
o Example: A class responsible for handling all database interactions (CRUD
operations).
2. Sequential Cohesion:
o Elements in a module are related such that the output of one function is the
input of the next.
o Example: A data processing pipeline where one step processes data, and the
next step formats it.
3. Communicational Cohesion:
o The module elements work on the same data or contribute to producing the
same output.
o Example: A function that reads, processes, and stores a file.
4. Procedural Cohesion:
o The module contains functions that are related but must be executed in a
specific order.
o Example: A function that first logs into a system, then retrieves data.
5. Temporal Cohesion:
o The elements are grouped together because they are executed at the same time
or during the same phase of execution.
o Example: Initialization routines in a system that all execute when the system
starts up.
6. Logical Cohesion:
o The module's elements are related logically but not necessarily functionally,
and it contains several tasks that are executed based on control flow (like an if-
else).
o Example: A utility class that performs different operations depending on some
configuration parameter.
7. Coincidental Cohesion (Lowest Cohesion):
o The elements have no meaningful relationship and are grouped arbitrarily.
This is undesirable.
o Example: A class that has unrelated methods like logging, network access, and
file I/O.
Coupling
Coupling refers to the degree of interdependence between different modules. It measures how
connected or reliant modules are on each other. Low coupling is preferable as it leads to
greater modularity, making changes in one module less likely to impact others.
Improved modularity.
Easier to refactor or replace individual components.
More flexible code that is easier to scale.
Reduced risk of unintended side effects when modifying one module.
High Cohesion, Low Coupling: This is the goal in good software design. Each
module should focus on a specific, well-defined task (high cohesion) while interacting
minimally with other modules (low coupling).
Trade-offs: Sometimes, increasing cohesion might inadvertently increase coupling
(e.g., if a module becomes highly cohesive but tightly integrated with another). The
goal is to strike a balance where the system remains modular and easy to maintain.
Examples:
Key Takeaways:
1. High cohesion ensures that each module is focused and easier to manage.
2. Low coupling ensures that modules are more independent and flexible, allowing for
easier maintenance and upgrades.
3. Aim for high cohesion and low coupling for better software design that is modular,
scalable, and maintainable.