![]() ![]() The switch will still have to grow when new employment types are added.We still have a switch which allegedly does "N-things".Public Money calculatePay(int employeeType) How do you do that? A switch of course! So you end up with code something like this: public Employee createEmployee(int employeeType) That way you could avoid the switch altogether and just call e.calculatePay().īut if the input really is an enum value as in the example, then you need a way to get the appropriate Employee-subclass given this enum value. Employee could have subclasses HourlyEmployee, SalariedEmployee etc., which override a calculatePay() method. The idiomatic way to do this in object oriented programming is to use subclasses and overloading. The switch check a type-field and then selects the method to call based on that. That said, there are reasons to avoid this particular use of a switch. But the cases here calculateCommissionedPay(), calculateHourlyPay() do seem to be on the same abstraction level, so this contradicts his general criticism of switch-statments as always doing N-things. Martin has a different definition which is that a method does "one thing" when all operations are on the same abstraction level. If you follow this thinking, you will quickly realize a program will never be able to do anything! This implies that any single method call is "one thing". In the passage Martin states that a switch does one thing per case and therefore by definition does N things. Martin's concept of "do one thing" is overly ambiguous to the point I believe it does more harm than good. How does the switch statement do more than one thing? ![]() But possibly the worst problem with this function is that there are an unlimited number of other functions that will have the same structure. Fourth, it violates the Open Closed Principle(OCP) because it must change whenever new types are added. Third, it violates the Single Responsibility Principle(SRP) because there is more than one reason for it to change. Second, it very clear does more than more thing. First, it's large, and when new employee types are added, it will grow. There are several problems with this function. Isn't it doing the "One Thing" mentioned by Uncle Bob? Here we have a function: public Money calculatePay(Employee e)įrom my inexperienced point of view, I can see that the switch statement inside the function calculatePay is only returning things based on the object Employee. This doubt is about Switch Statements from Chapter 3: Functions of the book named Clean Code ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |