Single Responsibility Principle: Definition and Core Problem
The Core Problem SRP Solves
When a class handles multiple responsibilities, changes to one responsibility can break or affect the others. This creates fragile code where a bug fix in one area introduces bugs in unrelated areas. SRP reduces coupling between unrelated concerns and makes code easier to test and maintain.
What is a "Responsibility"?
A responsibility is a reason to change. It represents a business concern or actor in the system. For example, in a Vehicle class, parking logic (facility management concern) and maintenance scheduling (operations concern) are separate responsibilities because they serve different stakeholders and change for different reasons.
Common Misconceptions
First, SRP does not mean a class should do only one thing or have only one method. A ParkingSpot class can have occupy(), vacate(), and isAvailable() methods because they all serve the single responsibility of managing spot state.
Second, responsibility is not the same as functionality. Validating data, persisting data, and formatting data are three separate responsibilities even though they might all involve the same entity.
Business Impact
SRP directly affects maintainability costs. When responsibilities are mixed, a change request from the accounting department (like fee calculation) might accidentally break parking availability logic (facility management). Separating these concerns isolates changes to their relevant modules.