
Single Responsibility Doesn't Mean 'Do One Thing'
- كاتب موثّق
محتويات المقال
RegularHours() helper in the Employee class, the pay tests pass, and the change ships. At the end of the month the operations team escalates: the hours report their auditors rely on shows different numbers, and nobody asked for that. The class was small and every method "did one thing", yet a change for one department still broke another department's work.
The common misconception
CalculatePay, ReportHours and Save does three things, so split it; a class with a single public method follows SRP by definition.
What actually happens
One reason to change, and reasons are people
"Do one thing" is a different rule at a different scale
Employee can be built from one-line methods and still answer to three departments. And a class with a dozen methods can respect SRP, as long as one group asks for all of them.
It's cohesion and coupling, restated
Why mixing groups hurts
calculatePay, accidentally break reportHours, and the COO starts demanding that you never change calculatePay again. In his 2020 follow-up he adds that microservices don't solve this: you can create a tangled service, or a tangled set of services, by mixing code that changes for different reasons.
The fix: split by who asks, not by verb
// One class, three audiences: finance, auditors, the database teampublic sealed class Employee(TimeCard card, decimal rate){ // Finance changed this to "overtime after 40 hours a week" private decimal RegularHours() => Math.Min(card.WeeklyHours, 40m); public decimal CalculatePay() => RegularHours() * rate + (card.WeeklyHours - RegularHours()) * rate * 1.5m; // The auditors' report reuses finance's helper, // so finance's change moved the auditors' numbers too public string ReportHours() => $"{card.EmployeeId}: {RegularHours()} regular hours"; public void Save(IEmployeeStore store) => store.Save(card);}// Finance's rules: only payroll asks for changes herepublic sealed class PayCalculator{ public decimal Calculate(TimeCard card, decimal rate) { var regular = Math.Min(card.WeeklyHours, 40m); var overtime = card.WeeklyHours - regular; return regular * rate + overtime * rate * 1.5m; }}// Operations' rules: only the auditors ask for changes herepublic sealed class HoursReport{ public string Line(TimeCard card) { var regular = Math.Min(card.WeeklyHours, card.ContractHours); return $"{card.EmployeeId}: {regular} regular hours"; }}TimeCard data. Saving moves to the persistence code behind IEmployeeStore, because the schema changes for technical reasons. Both classes compute "regular hours", which looks like duplication. It isn't: a 40-hour threshold and contracted hours are different rules that happened to agree, and they will change on different days for different people. Notice what we didn't do: split PayCalculator into RegularHoursCalculator, OvertimeCalculator and PayAssembler. Those pieces always change together, at finance's request, so splitting them would scatter one responsibility over three files.
How to apply it in your project
Name the actors For each important class, write down who asks for changes to it: a team, a role, a regulator. One name is a good sign. Two or more makes it a candidate for a split. Find helpers shared between actors Private methods or utilities called by code that serves different groups, like RegularHours(), are where one group's change leaks into another group's results.Split along actors, not verbs Move each group's rules into its own class or module and let them share only data. Accept that two rules may look alike for now. Keep "do one thing" for functions Inside each class, keep extracting small, well-named functions. That's readability work, and it doesn't change who the class answers to.
Key takeaways
SRP says a module should have one reason to change, and Martin says reasons to change are people. "Do one thing" is a separate rule for functions; following it doesn't guarantee SRP. Gather code that changes for the same reasons and separate code that changes for different reasons: that's cohesion and coupling. A helper shared by two groups' code is how one team's request breaks another team's feature. Don't split one group's rules into many tiny classes; that scatters a single responsibility.
التعليقات (0)