Invoice
- items
- total
+ calcTotal()
+ saveToDB()
+ toPdf()
+ emailTo(c)
The god class
1class Invoice {2 calcTotal(){ ... } // business math3 saveToDB(){ ... } // persistence4 toPdf(){ ... } // layout5 emailTo(c){ ... } // email6}
A class should have ONE reason to change. Bundle one responsibility per class; split it the moment a class starts juggling many.
1class Invoice {2 calcTotal(){ ... } // business math3 saveToDB(){ ... } // persistence4 toPdf(){ ... } // layout5 emailTo(c){ ... } // email6}
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?