Enumerations and Switch Statements
posted on 07/11/07 at 12:12:39 am by Joel Ross
Derik Whitaker posts about what he feels are best practices to using Enums in switch statements. He says that if the switch statement is the main path through a method, there should always be a default, and unless you have a specific thing to do in the default block, it should throw an exception.
Why is that a good thing? Because when you add new values to the enum, you get exceptions in places you didn't update your code. By throwing exceptions, it at least forces you to consider places where the enum is used, which is always a good thing.
Categories: Development