Skip to content

Single Responsibility

Easy — everyone uses thisArchitecture

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.