Interview frequency: …
Short answer
SRP says that a software module should have only one reason to change. The real measure is whether it serves one actor in the system.
Explanation
As Robert Martin (Uncle Bob) explains, SRP does not mean that a function does only one thing. It means a class is responsible to one person or group, such as a CFO, operations manager, or database administrator. If an Employee class contains calculatePay() (accounting responsibility), save() (infrastructure responsibility), and reportHours() (HR responsibility), a policy change from one group can threaten the class's stability for the others.
Code example
// Violation: a class combines salary calculation and database persistence
// Fix: separate responsibilities
class PayrollCalculator {
calculateNetSalary(emp: Employee): number { /*...*/ }
}
class EmployeeRepository {
save(emp: Employee): void { /*...*/ }
}Common mistakes
- Splitting code into tiny classes with one method each and calling it SRP, increasing cognitive complexity without benefit.