- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
... so TRWTF is the lack of attention to code formatting details on case 10?
No, it's a complete lack of inspiration as to what to map case 11 to.
Admin
Admin
Maybe one enemy has parts that drop off as you attack it that have to be spawned as smaller enemies. In that case, 11 kind of makes sense.
Admin
I'm twinging at "Bell Pepper" having a space, but "OneGrape" does not.
Admin
Well, you say game developers are not typically concerned with reusable code, but this thing is obviously borrowed from some fruit-oriented game with 12 enemy types, including one apparently very dangerous boss grape, and nobody enjoys writing serialization functions. Why reinvent the watermelon?
Admin
And we can tell it's dangerous because it's turned all the way up to ... eleven.
Admin
What was wrong with just using an array? (Aside from the complete lack of validation, that is.) Am I missing something?
Admin
In C# you use an enum (value type, usually derived from int) and just call the ToString() method. An array is an object type, it's slow and a waste of GC resources literally because it will end up just escalating in generations and the GC always have to make a pass over it - hence you want to avoid long living objects in general similiar to as you want to avoid allocing a lot of heap objects (aka reference types). With an enum the name is stored as type info meta data which the GC ignores by default.
In other words enums are way more efficient in every way, they are best practice and readable and you can add even more type information with attributes like DisplayName without losing out on refactoring capabilities or general overall performance and efficiency.
Admin
O(n) where there could be O(1) makes me hiss. In C++, I think I'd just do map<int,string>, along with a reverse mapping (array for int->string is appealing, but makes it harder to delete a specific ID later down the line). Not really knowing about C#, I do kind of like the enum approach in that language, although if I really want the spaces in the strings, I'd have to add in a replace and manage a special character. Also, I imagine it must also be storing some mapping permanently in memory in order to enact the conversions, so I don't see how memory is being handled any more minimally.
Admin
It's not O(n). Most compilers will optimize
switch
into a jump table. A good optimizer may even turn this whole function into an array reference.Admin
You're right! But I'll point out that while it runs in O(1), it's still O(n) code size!
Admin
Maybe the game in question it's a crossover between "Commander Keen: Keen Dreams" and "Eat Them To Defeat Them". Plus zombies. And one grape, provided by J.-B. E. Zorg...
Admin
Makes me realise how valuable it is that some languages force you to cover the entire range of a data type in your switch statement, or it won't compile.