You can think of the method above as an object belonging to the following class:
class PaymentCalculator implements Function<Employee, Money> { Money apply(Employee) { switch (e.type) { case COMMISSIONED: return calculateCommissionedPay(e); case HOURLY: return calculateHourlyPay(e); case SALARIED: return calculateSalariedPay(e); default: throw new InvalidEmployeeType(e.type); } } }
Then try to find the reasons why this class can be changed:
- changes in employee wages
- changes in the logic of calculations (new parameters like employee position, experience, etc.)
For at least these two types of changes, you will have to make corrections to this method. It is worth noting that the goal of SRP is to achieve low traction and a high degree of traction. To understand the benefits of this, try to imagine that you have a large system with hundreds of classes and methods like this: calculatePay, calculateVacation, createDepartment, etc. And all these classes and methods have such code. Will it be easy to make changes?
PS After you see the long if-else or case arguments, you can start thinking about SRP.
source share