Well, here goes.. potential downVote fodder, but enums is one of my favorite things..
Perspective
Focus on what's best for your design and code. Not using enums because a DB doesn't have that "data type?" OK. then Let's not make any custom classes for the same reason. That's nonsense.
enum goodness
(Note: I code in C#)
- Allows you to declare things in terms of your problem domain. A string is a string but a MonsterType enum IS a Monster (type).
- enum is strongly typed, essentially. Using a non-defined enum value is a compile error. OTOH, a string typo is another debugging session.
- I particularly like it for
switch
statements. Beats theHellOutta integers: switch(MonsterType)
- an enum can exhaustively define all valid values
- tends to be document-y
- Code quality enhancer - sure you have to recompile to add a new member, but that's deliberative, and missing a related code change will blow up (a good thing) vis-a-vis a new string value not handled somewhere that manifests as an elusive logic/processing error, requiring a debug session on your part.
- defaults to zero, not null. It's funny how the null concept can muck things up. Keep "null" in the database where it belongs.
- Pro coding tip: ALWAYS explicitly define a member for default. re:
MonsterType.Unknown = 0
. Your reward will be easier written code that is less buggy and easier to read. Your maintenance programmer will thank you.
string badness
- default value is null. It's a constant battle flip-flopping around null and "empty string". I see stupid, bug ridden code like this frequently:
if(string.IsNullorEmpty(myString.Trim()) ...
- if myString is null your program blows up with a runtime exception. Cannot happen with enums.
- string comparison is case sensitive
- error prone to typos
- "string" type is generic and in no way helps you express your problem in terms of your domain
- setting
myString = null
, fetched from the database IS NOT a variable with a value, it IS NOT even an object, but we try to treat it like it is!
integer obsfucation
if (MonsterType == 3)...
What the heck does that mean? Nuff said.
Here's a practical exercise. In your IDE, click on that "3" and ask it to "find definition." If your IDE could talk it would say "why don't you define the things in your problem domain instead of expecting me to guess what "3" means?"
Oh, I know. I'll declare a mess of constants somewhere.. somewhere. OMG. Let's pretend we're using enums! And it's more fun than working with a cohesive set of type safe values!
null drives me nuts
C# has a String.Empty
static property for a reason. It goes to not hijacking a valid value (space, for example) or null to represent an instantiated string object who's value is explicitly not any (valid) string. THIS is not the same thing as null.
null means nothing, literally. It means "go away, no one is home". Coders too often want it to mean "monster type unknown" for example, but for crying out loud define such concepts explicitly (see enum pro tip above). IMHO using null like this means your design is missing something.
null is the code zombie. It's walking around but it's nothing, dead, whatever. And it will bite you if you're not careful!
You will experience the never ending joy of deciding to store "empty" strings as a space character or as null;
and that a space is actually a valid string-domain value, not a "no value" value;
and that null and "empty string" really don't mean the same thing any way and trying to make them so causes problems when you need their "natural" functionality;
and the constant fussing w/in code converting from string.empty to null to space and vice versa to suit the task at hand. Over time your code will become inconsistent in this regard.
Hear me now and believe me later.