Part 6 of 7 · 1 min read
Not one thing
Two different rules.
SRP is not "do one thing"
CC has a different rule for functions: a function should do one thing, at one level of detail. That is good advice, and it is about the inside of a method. SRP is about classes and about people. A class can have many small methods that each do one thing, and still serve three actors. CA warns about this mix-up directly.
Use SRP in a round like this. Read each class you draw and ask, "who would ask to change this?". If the answer is "the pricing team and the notification team", split it.
CC adds a useful habit with the same aim. Describe the class in about twenty-five words without using the words "and", "or", "if" or "but". When the sentence will not come out clean, the class likely has more than one job.
Interview angle
Answer in your head first, then open it.
What is the single responsibility principle?
A class should be written for one actor, so one group's change request touches one class and cannot break another group's results.
How do you check a class for it?
List who would ask for changes. More than one group means split it.
Is SRP the same as "a function does one thing"?
No. That rule is about functions. SRP is about classes and the groups that change them.
Words you will meet
| Word | Meaning |
|---|---|
| Actor | A group of people who ask for changes for the same reason |
| Responsibility | Serving one actor |
| Accidental duplication | Code that looks alike now but belongs to different actors |
| Merge conflict | Two edits to one file that version control cannot join alone |
| Facade | A front object that hides several others behind one door |