I am quite confused on how to determine if a single method has one responsibility being done just like the following code from the book Clean Code
public Money calculatePay(Employee e) throws InvalidEmployeeType {
switch (e.type) {
case COMMISSIONED:
return calculateCommissionedPay(e);
case HOURLY:
return calculateHourlyPay(e);
case SALARIED:
return calculateSalariedPay(e);
default:
throw new InvalidEmployeeType(e.type);
}
}
as the author stated on this code snippet: "...clearly does more than one thing. Third, it violates the Single Responsibility Principle (SRP) because there is more than one reason for it to change.". On a first glance of the code I was thinking that how come that the method violates SRP since if there will a change on the code it would be the switch statement only if there will be an added employee type but as I try to understand the method further I came up with a hypothesis on why does it violates the said principle.
My hypothesis on this one would be that since the name of the method is calculatePay(Employee e)
then the only responsibility of this method is for the payment computation as the method's name suggest but since inside the method there is a Switch case on filtering the type of Employee this filtering is now a different or another responsibility thus violates SRP. I don't know if I get it right.
Aucun commentaire:
Enregistrer un commentaire