Concept · Core

Single Responsibility (S)

A class should have ONE reason to change. Bundle one responsibility per class; split it the moment a class starts juggling many.

Asked atAmazonGoogleAtlassian
step 1 / 8
1/2The god class
Invoice
- items
- total
+ calcTotal()
+ saveToDB()
+ toPdf()
+ emailTo(c)
The god class
1class Invoice {
2 calcTotal(){ ... } // business math
3 saveToDB(){ ... } // persistence
4 toPdf(){ ... } // layout
5 emailTo(c){ ... } // email
6}

Here is Invoice. It compiles, it works, and at a glance it looks like a perfectly normal class — just a busy one. So what is wrong with it?