0% completed
Cohesion and its Relation to the Single Responsibility Principle (SRP)
On This Page
What is Cohesion?
General Example: Garbage Dump vs. Organized Dustbins
Low Cohesion in Code: Single Class with Multiple Responsibilities
Increasing Cohesion: Splitting Responsibilities into Separate Classes
Cohesion and SRP
What is Cohesion?
Cohesion refers to how closely related and focused the responsibilities of a class, module, or method are.
A highly cohesive class will have methods that work together to achieve a single purpose. On the other hand, low cohesion occurs when a class has unrelated responsibilities bundled together, which makes it harder to maintain and understand.
General Example: Garbage Dump vs. Organized Dustbins
Imagine a garbage dump where every kind of waste is thrown together: plastic, paper, food waste, and more. This is like a class with low cohesion, where everything is mixed and there’s no clear purpose for the class. It’s hard to manage and sort out when you need something specific.
Now, think about separate dustbins for each type of waste, as shown in the diagram above: one for plastic, one for paper, and one for food waste. This is a class with high cohesion. Each dustbin (or class) has a clear responsibility and manages only the type of waste it’s meant for.
By organizing waste into specific bins, the system is much easier to manage, and that’s exactly what cohesion helps achieve in code.
Low Cohesion in Code: Single Class with Multiple Responsibilities
Let’s take a look at a coding example with low cohesion. Here, we have a single class that handles user data and file management, all in one place.
Run it and read the output. The class handles user management and file operations, and the two have nothing to do with each other. Its cohesion is low because the methods inside it do not serve one purpose.
Note what this costs. Anyone who wants to change how the audit file is written has to open the class that also owns adding and deleting users.
Increasing Cohesion: Splitting Responsibilities into Separate Classes
To improve cohesion and follow the SRP, we can split this class into smaller, more focused classes. One class will handle user management, and another will take care of file operations.
The four operations print exactly what they printed before. What changed is that each class now has one purpose: UserManager manages users and AuditFile writes and reads the audit log. Cohesion is high in both.
The caller does more work now, because it holds two objects rather than one. That is the price, and it is worth paying as soon as the two jobs start changing for different reasons.
Cohesion and SRP
To follow the Single Responsibility Principle, your classes need high cohesion. Why? Because high cohesion means that a class focuses on a single responsibility or task, which is exactly what SRP demands.
- Low cohesion often results in a class with multiple unrelated responsibilities, violating the SRP.
- High cohesion ensures that each class or module is centered around one responsibility, making the code easier to maintain and extend.
By organizing your code to have high cohesion, you naturally follow the SRP, as each class or component will focus on a single, clearly defined responsibility.
Rohit Bhanot
· 14 days ago
I think the example is wrong for Cohesion and is more apt for High Coupling rather than Low Cohesion.
The right example for low cohesion would be if one of the UserAdd or UserDelete methods lives in a separate class from UserManager class, why, because then all of the user related functionalities are not living together and hence low cohesion.
What the example is demonstrating is mixing two unrelated functionalities in a single class which leads to High Coupling and violating SRP, so breaking them into separate classes is the correct approach.
I think you should fix it, this can give wrong understanding to readers !
Reading Progress
0%
On This Page
What is Cohesion?
General Example: Garbage Dump vs. Organized Dustbins
Low Cohesion in Code: Single Class with Multiple Responsibilities
Increasing Cohesion: Splitting Responsibilities into Separate Classes
Cohesion and SRP