Friday, 15 July 2011

enums - Using Integer vs String for a "type" value (Database and class design) -



enums - Using Integer vs String for a "type" value (Database and class design) -

i've been developing few mobile games, in games fetch info server-database.

i'm used storing "type" values integer identifier, , enum in client identify info coming server.

as example: on database table:

monsters table: monsterid(int), name(string), monstertype(int)

on client-sided code:

typedef enum { monstertypeground = 1, monstertypeaquatic = 2, monstertypeamphibious = 3, monstertypeaerial = 4 }monstertype;

note above code objective-c, in can assign integer values. i'm using c# , c++.

however, our database guy said enums compiler trick, there’s no such thing enums info types. argued integer type-identifiers create hard people(other developers) understand values mean, , cannot perchance know equivalents without looking @ client-sided code , enums aren't because need create sure enum syncs server-side ids , it's improve utilize strings instead.

my question is: there objectively right reply on matter?

are there alternatives aside using enums on client-code still utilize integers server database?

long answer

your database guy wrong. there, of course, if enum info type. provided 1 in example. , mysql knows of it. , lots of programming languages have enum.

but right, in enums (always?) optimized compilers. if there 4 choices represented 1 through 4, happen find identifiable strings easier read in code. compilers have no such problems , in fact don't care number. aerial monster type 4 , flying spaghetti monster type 4. easier order of several magnitudes cpu compare bytes compared comparing strings.

also, right having enum in c code or whatever code can problem:

if alter definitions (especially order), need recompile programme , linked programs if library. pain. if need interface different languages, can pain. need sync multiple definitions. managing enums cumbersome, removing types.

you can work around having function translates strings enums or other way around.

but there benefits:

if alter name of monster type, aerial monster can remain type 4, if rename them flying monsters. info consistency in database guarenteed, because string. if utilize strings, there no painless way of converting. well, find , replace in code, not in databases. it eficient format. save 10 bytes. experience matters, except if have tens of millions of entries. tl;dr

no there no objective answer.

if find programmer ease important, strings can improve option. if find compiler optimization important, enum improve option.

my sentiment compiler optimization important, scarce programmer time is. myself utilize strings, except in databases.

so yes, guy has point.

database-design enums class-design value-type

No comments:

Post a Comment