Cohesion and Coupling
What are cohesion and coupling?
Cohesion and coupling in software engineering are two important concepts that describe the relationship between the modules or components in a software system.
They are used to assess the quality of the design & to measure how well the system's components interact with each other.
Coupling represents the relationships between modules, and cohesion defines the relationships within them.
Cohesion
Cohesion in software engineering refers to the degree to which the responsibilities of a single module are related & focussed.
It measures how strongly the internal elements of a module are connected & work together to achieve a common purpose.
High cohesion in software engineering promotes reusability, maintainability, and understandability of the code.
The higher the degree of cohesion, better the quality of the software.
Coupling
Coupling in software engineering refers to the degree of interdependence between two or more modules in a system.
It measures how closely the modules are connected & how much they rely on each other.
Low Coupling in software engineering promotes independence, modularity, and flexibility of the code.
The lower the degree of coupling, better the quality of the software.
What is cohesion?
Cohesion in software engineering refers to the degree to which the elements inside a module/components belong together, directed towards performing a single task.
In simple words, it is the degree to which the elements of the module are functionally related.
A module with high cohesion encompasses elements tightly related to each other and united in their purpose.
But, a module is said to have low cohesion if it contains unrelated elements, and if the module has all related elements, it is considered to have high cohesion.
A good software design will have high cohesion.
The concept of cohesion is closely related to the Single Responsibility Principle, where a class should only have one responsibility or should perform a single task.
The modules following the SRP are likely to have high cohesion as they are meant to perform a single task in the software.
What are the different types of Cohesion in software engineering?
The types of Cohesion in software engineering are:
1. Functional Cohesion
On of the types of cohesion in software engineering is functional cohesion that occurs when the elements within a module perform a single, well-defined task or a function.
All the elements within the module contribute to achieving the same objective.
This type of cohesion is considered the most desirable & strongest.
Examples - reading transaction records, cosine angle computation, seat assignment to an airline passenger, etc
2. Sequential Cohesion
Sequential cohesion occurs when the elements within a module are arranged in a specific sequence, with the output of one element serves as the input for the next element.
The elements are executed in a step-by-step manner to achieve a particular functionality.
Examples - cross-validated records and formatting of a module, raw records usage, formatting of raw records, cross-validation of fields in raw records, etc.
3. Communicational Cohesion
Communicational cohesion is anohter one of the types of cohesion in software engineering occurs when the elements within a module operate on the same input data or share data through parameters.
The elements within the module work together by passing data to each other.
It is weaker than sequential cohesion.
Example - usage of a customer account number, finding the name of the customer, the loan balance of the customer, etc.
4. Procedural Cohesion
Procedural cohesion occurs when the elements within a module are grouped based on a specific sequence of actions or steps.
This type of cohesion is weaker than the communicational cohesion.
Examples - Read, write, edit of the module, zero padding to the numeric fields, returning records, etc.
5. Temporal Cohesion
Temporal cohesion occurs when the elements within a module are executed at the same time or within the same timeframe.
It is considered weaker than the procedural cohesion.
Examples - Setting the counter to zero, opening the student file, clearing the variables of error messages, initializing the array, etc.
6. Logical Cohesion
Logical cohesion occurs when the elements within a module are logically related, but do not fit into any other cohesion types.
It is not strong as other cohesion types.
Examples - When a component reads inputs from tape, disk, and network, etc.
7. Coincidental Cohesion
Coincidental cohesion occurs when the elements are not related to each other.
Examples - Module for miscellaneous functions, customer record usage, displaying of customer records, calculation of total sales, and reading the transaction record, etc.
What is Coupling?
Coupling in software engineering refers to the degree of interdependence & connection between modules or components within a software system.
Two modules are said to have high coupling if they are closely connected.
In simple words, coupling is not just about modules, but the connection between modules and the degree of interaction or interdependence between the two modules. If two modules contain a good amount of data, then they are highly interdependent.
If the connection between components is strong, we speak about strongly coupled modules; when the connection is weak, we speak about the loosely coupled modules.
What are the different types of Coupling in software engineering?
Here are the 6 types of coupling in software engineering:
1. Data Coupling
Data coupling is a type of coupling in software engineering that occurs when modules share data through parameters or arguments.
Each module maintains its own data and does not exactly access or modify the data of other modules.
This type of coupling promotes encapsulation & module interdependence.
2. Stamp Coupling
Stamp coupling is a weaker form of coupling where modules share a composite data structure, but not all the elements are used by each module.
As the data and elements are pre-organized and well-placed beforehand, no junk or unused data is shared or passed between the two coupling modules which improves the efficiency of the modules.
3. Control Coupling
Control coupling is one of the types of coupling in software engineering occurs when one module controls the behavior of another module.
This type of coupling implies that one module has knowledge of internal workings & decisions or another module, that makes the code more difficult to maintain.
4. External Coupling
External coupling measures the degree to which the system relies on external entities to fulfill its functionality or interact with the external environment.
Low external coupling - Changes in the external entities have little impact on internal implementation of the system.
Medium external coupling - Changes in the external entities require some modifications within the system to accomodate new interfaces.
High external coupling - Changes in the external entities have a substantial impact on internal implementation of the system, requiring extensive modifications.
5. Common Coupling
Common coupling occurs when two or more modules in the system share global data.
The modules can access & manipulate the same global variables & the data structures.
6. Content Coupling
Content coupling is another type of coupling in software engineering that occurs when one module directly accesses or modifies the content of another module.
This type of coupling is strong & undesirable since it tightly couples the modules, making them highly independent on each other's implementation.
What is the relationship between Cohesion and Coupling in software engineering?
The relationship between coupling and cohesion in software engineering can be determined by these factors:
Inverse Relationship - When Cohesion is high, Coupling tends to be low. High Cohesion means that the elements within a module are closely related & work together to achieve a defined purpose.
Design Objective - High Cohesion & Low Coupling contribute towards improved maintainability, reusability, & the flexibility of the software system.
Impact on Software Quality - High Cohesion contributes to better understandability, readability, & maintainability of the codebase. Low Coupling reduces the interdependencies between the modules.
Trade-Offs - Developers need to strike a balance based on the specific requirements & the constraints of the system.
What are the metrics for Cohesion & Coupling in software engineering?
These are quantitative measures for assessing the level of cohesion and coupling in software engineering. Here are some of the common metrics:
Cohesion metrics
1. Lack of Cohesion of Methods (LCOM)
Calculated by the number of disjoint sets of methods in the module.
A higher LCOM value indicates lower cohesion.
2. LCOM2
Calculated by the number of disjoint sets of methods - but provides a more refined measure of cohesion compared to LCOM.
3. Tight Class Cohesion (TCC)
Represents the proportion of method pairs that access the same attributes.
A higher TCC value indicates stronger cohesion in software engineering.
Coupling metrics
1. Coupling Between Objects (CBO)
CBO measures the no. of other classes or objects to which a class is coupled.
Higher CBO values suggest increased coupling & reduced reusability.
2. Response For a Class (RFC)
RFC measures the no. of methods in a class that can be executed in response to a message received by an object of that class.
Higher RFC values indicate higher complexity.
3. Depth of Inheritance Tree (DIT)
DIT measures the no. of levels in the inheritance hierarchy for a class.
Higher DIT values suggest a deeper inheritance tree & increased coupling.
Last updated