3 ms·
In a lot of ways, I think the map solution is actually worse. There isn't an easy way to figure out what code is used for a given enum value with the map alone.
by slaymaker1907 6y ago
In a lot of ways, I think the map solution is actually worse. There isn't an easy way to figure out what code is used for a given enum value with the map alone. The searchability depends on how the map is initialized. Not to mention a map will always introduce null or option types that you have to handle.
A map can be a good solution though, particularly if it's something like mapping enum values to strings that is constructed via EnumClass.values().map... or something so that you know the map is a total function.
- gurkendoktor 6y agoUsing a map also completely sidesteps the point of cyclomatic complexity because there are no code paths to count anymore; now they're data. And even though the function does the same as before, the linter will congratulate you on the reduced complexity.
- Twisol 6y agoAnd you'd have essentially replaced statically understood control flow with the equivalent of a computed goto. It's not like it's a bad approach, but from a code path standpoint, it's unequivocally worse, not better. (Imagine if that map was mutable!)
- Leherenn 6y ago> Not to mention a map will always introduce null or option types that you have to handle. It is the same with the switch, at least in C/C++. You either have a default, or must list all the possible cases and still return something "at the bottom".
- slaymaker1907 6y agoThis is becoming an increasingly less common strategy for languages to take given the popularity of sum types.