I've been developing a few mobile games, in which those games fetch their data from a server-database.
I'm used to storing "type" values as an integer identifier, and an enum in the client to identify the data coming from the server.
As an example: On the database table:
Monsters Table: MonsterId(int), Name(string), MonsterType(int)
On the client-sided code:
typedef enum {
MonsterTypeGround = 1,
MonsterTypeAquatic = 2,
MonsterTypeAmphibious = 3,
MonsterTypeAerial = 4
}MonsterType;
Note that the above code is Objective-C, in which I can assign integer values. But I'm also using C# and C++.
However, our database guy said that enums are a compiler trick, there’s no such thing as enums data types. He argued that integer type-identifiers make it hard for people(other developers) to understand what the values mean, and that they cannot possibly know the equivalents without looking at the client-sided code and that enums aren't good because you need to make sure the enum syncs with server-side ids and that it's better to use strings instead.
My question is: Is there an objectively correct answer on the matter?
Are there any alternatives aside from using enums on the client-code but will still use integers for the server database?
Your database guy is obviously wrong. There, of course, if something like an ENUM data type. You provided one in your example. And MySQL knows of it. And lots of programming languages have something like an ENUM.
But he is also right, in that ENUMs are often (always?) optimized by compilers. If there are four choices that could be represented perfectly by 1 through 4, but we happen to find identifiable strings easier to read in code. But compilers have no such problems and in fact don't care about the number. The Aerial monster is type 4 and the Flying Spaghetti Monster is type 4. It is even easier by an order of several magnitudes for a CPU to compare bytes compared to comparing strings.
Also, he is right that having an ENUM in C code or whatever code can be a problem:
You can work around this by having a function that translates strings to enums or the other way around.
But there are also benefits:
No there is no objective answer.
If you find programmer ease most important, then strings can be a better option. If you find compiler optimization most important, then an enum is a better option.
My opinion is that compiler optimization is rarely important, but scarce programmer time is. I myself mostly use strings, except in certain databases.
So yes, your guy has a point.
Well, here goes.. potential downVote fodder, but enums is one of my favorite things..
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.
(Note: I code in C#)
switch
statements. Beats theHellOutta integers: switch(MonsterType)
MonsterType.Unknown = 0
. Your reward will be easier written code that is less buggy and easier to read. Your maintenance programmer will thank you.if(string.IsNullorEmpty(myString.Trim()) ...
- if myString is null your program blows up with a runtime exception. Cannot happen with enums.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!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!
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.
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