SOLID: S — Single Responsibility Principle
A class should have one reason to change: one job.
Why it matters
When one class does several jobs, unrelated changes collide: a change to the email wording risks breaking the tax calculation, and every change means retesting everything.
The idea
If one class holds the business rules, the database saving and the email sending, a change to any one of them risks breaking the others. Split it so each class does one thing well and changes for one reason.
A restaurant where the chef cooks, the cashier takes payments and the cleaner cleans. If the chef also did the accounts, every new tax rule would interrupt the cooking.
In a tiny café one person does everything, and that's fine. Splitting too early into very small classes adds complexity; apply this when responsibilities really do change separately.
How it works
- List what the class does, and who asks for changes to each part (finance, the database team, marketing).
- Each different 'who' is a separate responsibility.
- Move each responsibility into its own class with a clear name.
- Connect them in a small coordinating function or service.
Example
Invoice calculates the totals, InvoiceRepository saves it, InvoiceEmailer sends it.
Key points
- 'A reason to change' usually means a different person or team asking for changes.
- Warning signs: huge classes, many unrelated imports, methods that never use each other's data.
- The result: smaller classes that are easier to test.
Common mistakes
- Splitting by the number of methods instead of by reason to change.
- Creating dozens of tiny classes that are always changed together.
Not true: Single responsibility: one method per class.
In fact: It means one reason to change: one person or team who would ask for changes. A class can have many methods that serve that one purpose.
In the interview
Single responsibility means each class has one reason to change. For example, I'd separate invoice calculation, saving and emailing, so a change to the email template can't break the tax logic.
Part of Unit 5: Low-level design: OOP and SOLID in the free System Design course.