Back

Single Responsibility

The S in SOLID: a class or module should have only one reason to change. If a class handles both business logic and saving to the database, a change to either one forces you to touch it.

// Bad: Invoice handles math AND saving AND printing
class Invoice {
  calculateTotal() { /* ... */ }
  saveToDatabase() { /* ... */ }
  printPDF() { /* ... */ }
}

// Good: one responsibility each
class Invoice {
  calculateTotal() { /* ... */ }
}
class InvoiceRepository {
  save(invoice: Invoice) { /* ... */ }
}
class InvoicePrinter {
  print(invoice: Invoice) { /* ... */ }
}

Roadmap