3 ms·
String in Switch statement I don't see this as an improvement. A good practice is to use Enums for case/switch. Using String is harder to maintain.
by miahi 14y ago
String in Switch statement
I don't see this as an improvement. A good practice is to use Enums for case/switch. Using String is harder to maintain.
- alexatkeplar 14y agoJust saw this - I made a related comment above.
- Joe8Bit 14y agoMaybe a naive question, please forgive if it is, but why does using a string make it harder to maintain?
- InclinedPlane 14y agoEnums are more "DRY"
- miahi 14y agoAs InclinedPlane said, Enums are more DRY and less error-prone. Even if you have to parse a String to an Enum, you only have to do it in one place (in the Enum definition), and then you only work with known objects. Also, if you want to refactor/auto replace, with an Enum object you are sure the value is correctly replaced everywhere (as the IDE knows to handle Enum), but if you want to replace a String you have to check all the occurrences of the string by hand, because some of them may be used for other purposes.
- mbell 14y agoCould have been nice, if they supported other types of matching. e.g. contains or regex. The vast majority of the time when I want to switch on a String I'm parsing a URL in a session filter into several categories or something like that.