• Bittle Tobby Lables (unregistered)

    ... 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.

  • Michael R (unregistered)
            default: returnData = "Frist";
            break; 
    
  • (nodebb) in reply to Bittle Tobby Lables

    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.

  • Chris (unregistered)

    I'm twinging at "Bell Pepper" having a space, but "OneGrape" does not.

  • Chris (unregistered)
    Comment held for moderation.
  • Duston (unregistered)
    Comment held for moderation.
  • Dave (unregistered) in reply to Chris
    Comment held for moderation.
  • Zygo (unregistered)

    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?

  • (nodebb)

    including one apparently very dangerous boss grape

    And we can tell it's dangerous because it's turned all the way up to ... eleven.

  • OldCoder (unregistered)

    What was wrong with just using an array? (Aside from the complete lack of validation, that is.) Am I missing something?

  • MaxiTB (unregistered) in reply to OldCoder

    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.

  • Yikes (unregistered)

    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.

    using System;
    
    class Program {
      enum EnemyType {
        Radish = 0,
        Tomato,
        Orange,
        Banana,
        Strawberry,
        Carrot,
        Watermelon,
        Grapes,
        Broccoli,
        Bell_Pepper,
        Coconut,
        OneGrape
      };
    
      public static string enemyIdNumToName(int intId) {
        if (EnemyType.IsDefined(typeof(EnemyType), intId)) {
          return ((EnemyType)intId).ToString();
        }
        else {
          return "UNDEFINED";
        }
      }
    
      public static int enemyIdNameToNum(string name) {
        if (EnemyType.IsDefined(typeof(EnemyType), name)) {
          return (int) Enum.Parse(typeof(EnemyType), name);
        }
        else {
          return -1;
        }
      }
    
      static void tryID(int intID) {
        string name = enemyIdNumToName(intID);
        int num = enemyIdNameToNum(name);
        Console.WriteLine("intID=" + intID + "  name=" + name + "  reverse id=" + num);
        
      }
      
      static void Main(string[] args) {
        tryID(11);
        tryID(12);
      }
    }
    
    
  • Barry Margolin (github) in reply to Yikes

    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.

  • Yikes (unregistered) in reply to Barry Margolin

    You're right! But I'll point out that while it runs in O(1), it's still O(n) code size!

  • Officer Johnny Holzkopf (unregistered) in reply to Zygo

    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...

  • Chris (unregistered) in reply to Yikes
    Comment held for moderation.
  • ismo (unregistered) in reply to Michael R
    Comment held for moderation.
  • Michael R (unregistered) in reply to OldCoder
    Comment held for moderation.
  • RLB (unregistered)
    Comment held for moderation.
  • Craig (unregistered) in reply to MaxiTB
    Comment held for moderation.
  • Altreus (unregistered)

    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.

  • baloch (unregistered)
    Comment held for moderation.
  • Allocator (unregistered) in reply to Yikes
    Comment held for moderation.
  • Gnasher729 (unregistered) in reply to Yikes
    Comment held for moderation.

Leave a comment on “Enumerating Your Plants”

Log In or post as a guest

Replying to comment #:

« Return to Article