Single Responsibility
ELI5 — The Vibe Check
Single Responsibility means every class or function should do ONE thing and do it well. A class called UserEmailPasswordDatabaseLogger is screaming 'I do too much!' Split it up. A chef shouldn't also fix the plumbing.
Real Talk
The Single Responsibility Principle (SRP) states that a class or module should have only one reason to change — meaning it should encapsulate only one aspect of functionality. Violating SRP creates fragile, tightly coupled code that breaks when unrelated requirements change.
Show Me The Code
// Bad: two reasons to change
class User {
save() { /* DB logic */ }
sendWelcomeEmail() { /* Email logic */ }
}
// Good: one responsibility each
class UserRepository { save(user) { /* DB logic */ } }
class UserEmailService { sendWelcome(user) { /* Email logic */ } }
When You'll Hear This
"This class violates Single Responsibility — split the email logic out." / "SRP keeps classes small and focused."
Related Terms
Cohesion
Cohesion is how well the things inside a module belong together. High cohesion means all the stuff in a class is related — like a toolbox full of tools.
Refactoring
Refactoring is improving the internal structure of code WITHOUT changing what it does from the outside.
Separation of Concerns
Separation of Concerns means different parts of your code should handle different concerns and not step on each other's toes.
SOLID (SOLID)
SOLID is five rules for writing code that doesn't turn into a nightmare over time. Each letter stands for a different rule.