3 ms·
> An enum that's rarely or never the subject of an if/switch check or pattern match is probably OK. If its never, then it is probably pointless. If it is rare
by dragonwriter 5y ago
> An enum that's rarely or never the subject of an if/switch check or pattern match is probably OK.
If its never, then it is probably pointless.
If it is rare, it is probably OK irrespective of language.
If it is common, and it is a language with strong OOP support, and the conditionals repeat each other, you should probably consider whether you are using the enum as an inverted class and implementing common behavior that would make more sense in methods via redundant and hard to maintain conditionals on the enum. (If not, you should also consider if the enum is appropriate but the conditionals should be abstracted to a function.)
- notacoward 5y ago> If its never, then it is probably pointless. I disagree, because using an enum value as an index into an array or map (e.g. the scheduler-class example, or any other kind of grouping/routing) can still be useful even when there's no if/switch in sight. These patterns occur very frequently IMX. In many of these cases proper subranges would be even better and I wish more languages supported those, but an enum is still better than resorting to naked integers or (ick) strings to represent a small set of discrete values.