Until asking a question on here I never considered (enums) to be a "bad thing." For those out there that consider them not to be best practice, what are some approachs/patterns for avoiding their use in code?
Edit:
public Enum SomeStatus
Approved = 1
Denied = 2
Pending =3
end Enum
If you have a fixed set of options, such as your example, then strong typed enums are a good thing, and should not be avoided. But beware they don't actually provide type safety other than enforcing that the value be of the type of the enum's underlying type.
In most cases, enums smell because it's frequently abused, but that doesn't mean that you have to avoid them. Enums can be a powerful tool in your arsenal if used properly.
The problem with enums is described in Fowler's Refactoring, where it is considered a code smell. It has nothing to do with type safety, but rather that it forces you to sprinkle switch
statements all over your code, thus violating the DRY Principle.
The State pattern is a better model of the same structure because it lets you implement and vary the logic related to the same state in the same class. This also increases cohesion and lessens class coupling.
I think using an enum is a good thing. It provides strong type safety.
They have some disadvantages at times, but this is really typically related to situations where every possible option is not known in advance. If you have a fixed set of options, such as your example, then strong typed enums are a good thing, and should not be avoided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With