0% found this document useful (0 votes)
241 views3 pages

Cohesion and Coupling in Software Engineering

Uploaded by

Balaji Bala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views3 pages

Cohesion and Coupling in Software Engineering

Uploaded by

Balaji Bala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Types of Cohesion (from high to low):

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.

Key Characteristics of High Cohesion:

 Simpler understanding of the module’s functionality.


 Easier to maintain and test.
 Lower redundancy.
 Higher reusability.

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.

Types of Coupling (from low to high):

1. Data Coupling (Low Coupling):


o Modules share data through parameters. They are independent as long as the
data format remains unchanged.
o Example: Two functions where one passes data to the other via function
arguments.
2. Stamp Coupling (Medium):
o Modules share complex data structures, but not all parts of the data structure
are used.
o Example: A function that passes an entire object, but only a few attributes are
used by the receiving function.
3. Control Coupling:
o One module controls the behavior of another by passing control information
(e.g., flags).
o Example: A function passes a flag that tells another function how to behave.
4. External Coupling:
o Modules are dependent on external systems or devices (e.g., database
connections, file systems).
o Example: Two modules that both depend on a specific external library or
framework.
5. Common Coupling (High):
o Multiple modules share global variables. A change in the global variable can
affect many modules.
o Example: Several classes sharing and manipulating the same global state.
6. Content Coupling (Highest Coupling):
o One module directly modifies or relies on the internal workings (such as
variables or methods) of another module.
o Example: A function that modifies local data in another function.
o
Key Characteristics of Low Coupling:

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

Balancing Cohesion and Coupling

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

 High Cohesion, Low Coupling:


o A module responsible solely for sending emails, which receives email content
and recipient details via parameters.
 Low Cohesion, High Coupling:
o A module that both handles database connections and performs complex data
transformations, while also sharing global variables with another module.

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.

You might also like