Card 08/ 08
Recap: Single Responsibility
Count the people who ask for changes, not the methods, and split along them.
The single responsibility principle gives each class one reason to change.
- A responsibility is an actor: a group of people who ask for changes. Count actors, not methods.
- The symptom is a change for one group breaking the work of another, as finance's rounding broke marketing's layout.
- The refactor moves each actor's code into its own class, one step at a time, checking the output after each step.
- Accidental duplication is code that looks shared but serves two actors; give each actor its own copy, as with payroll's and HR's regular hours.
- Libraries split the same way: Python's
loggingseparates what to record, where it goes and how it looks. - Over-splitting is the opposite mistake. Code that always changes together for the same people belongs together, however many methods it needs.
← → move through sections and cards