Beware of switch

added by AndreiR23
7/25/2012 8:25:59 AM

5 Kicks, 548 Views

If you find yourself writing switches in one or more properties or methods of the class, switching on the same thing (PersonType here) then stop it. You are writing hard-to-maintain and error-prone code. Plus its inelegant. This practically is a cry for polymorphism.


7/25/2012 8:26:32 AM
You could also go another direction, depending on the complexity of your classes, and use external configuration rather than either enums or classes.

7/25/2012 9:17:26 AM
Definitely! In this example that I've presented I should have had a Role class so I can reassign a person more easily and I would have modeled the domain better.