So I was looking at some code that was checked in and I got all puzzled over:
// Amount of days before cancellation can't be done
enum Cancellation { Limit = 2 };
Asking the guy who checked it in he argued that it's much better to use enums instead of static variables, bettern than this:
private static int CANCELLATION_LIMIT = 2;
So we started arguing. My argument was that he was using enum as a way to store values (it'll break if there were two enum symbols with the same value). He argued it was an antipattern to have static variables in a class.
My question is what best practice should be used for either?
Best Solution
Enums are typed.
That is, if you have a method where you have to pass a certain 'state' to a method for instance, you can only pass 'valid' arguments. For instance:
This is a good example -imho- of using enums. When OrderState is an int for which you create 2 const ints, you have no restriction and are able to pass invalid values. The compiler won't complain.
However, the case that you're bringing up, I think using enums is not a valid solution. It's a misuse of using an int, and a const int should be used.
Enums are good, but they should be used where they must be used. They're not the preferred tool in every situation. Having a const or static var is in this case not an antipattern.