Cohesion and SRP
What you'll learn: How the Single Responsibility Principle directly promotes high cohesion by grouping related functionality together and keeping unrelated concerns separate.
The Natural Partnership
Remember learning about cohesion—how closely related the responsibilities within a module are? And the Single Responsibility Principle (SRP)—that each class should have one reason to change? These two concepts are deeply connected. In fact, SRP is the practical application of the goal of high cohesion.
When you follow SRP, you automatically increase cohesion. By giving a class only one responsibility, everything inside that class naturally relates to the same purpose. It's like organizing a toolbox: when all the screwdrivers are together in one compartment, that compartment has high cohesion—everything serves the same function.
Why They Work Together
High cohesion means the methods and data in a class work together toward a single, well-defined purpose. SRP enforces this by asking: "Does this class have more than one reason to change?" If it does, those are likely unrelated concerns that should be separated.
Consider a class handling both user authentication and email notifications. It has low cohesion because authentication logic and email logic are unrelated. Applying SRP, you'd split this into two classes: Authenticator and EmailNotifier. Now each class has high cohesion—all methods in Authenticator relate to authentication, and all methods in EmailNotifier relate to sending emails.
The Inverse Relationship
SRP also prevents low cohesion. When you dump multiple unrelated responsibilities into one class (violating SRP), you create a class where methods don't naturally work together. The class becomes a grab-bag of functionality with weak internal relationships.
Key Takeaway: Following the Single Responsibility Principle naturally creates highly cohesive classes where all parts work toward the same purpose, while separating unrelated concerns into different classes.