Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Enums are for wimps

Enums are for wimps

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorial
15 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Bernhard Hiller

    Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:

    private int DecodeElementType(string elementName)
    {
    switch (elementName)
    {
    case "Blah": return 1;
    case "Blub": return 2;
    case "Hmpf": return 3;
    case "Grml": return 4;
    case "Tralala": return 5;
    default: return 0; // unknown
    }
    }

    I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...

    D Offline
    D Offline
    den2k88
    wrote on last edited by
    #6

    Implicit enums are ever worse.

    public enum Implicit
    AA_0
    BB_1
    CC_2
    DD_3
    EE_4
    GG_6
    HH_22
    II_7
    LL_17
    ...
    end enum

    Guess what value did HH_22 have? And GG_6? BTW the numbers in the end are useful since this enum maps fixed alarm codes that come from a PLC. The only reason the code worked was because the same creator of this enum didn't use its values but used magic numbers all over the code. Half a day hunting for a bug because alarm 31 never matched with '== XX_31'. XX_31 equalled 30.

    * CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.

    J 1 Reply Last reply
    0
    • D Daniel Pfeffer

      Please use some other display method. As an engineer, I am "expression-blind" :sigh: (see [Engineers are cold and dead inside](https://www.codeproject.com/Messages/5364557/Engineers-are-cold-and-dead-inside.aspx))

      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

      D Offline
      D Offline
      David ONeil
      wrote on last edited by
      #7

      :laugh:

      Sudden Sun Death Syndrome (SSDS) is a very real concern which we should be raising awareness of. 156 billion suns die every year before they're just 1 billion years old. While the military are doing their part, it simply isn't enough to make the amount of nukes needed to save those poor stars. - TWI2T3D (Reddit)

      1 Reply Last reply
      0
      • D den2k88

        Implicit enums are ever worse.

        public enum Implicit
        AA_0
        BB_1
        CC_2
        DD_3
        EE_4
        GG_6
        HH_22
        II_7
        LL_17
        ...
        end enum

        Guess what value did HH_22 have? And GG_6? BTW the numbers in the end are useful since this enum maps fixed alarm codes that come from a PLC. The only reason the code worked was because the same creator of this enum didn't use its values but used magic numbers all over the code. Half a day hunting for a bug because alarm 31 never matched with '== XX_31'. XX_31 equalled 30.

        * CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.

        J Offline
        J Offline
        Jon McKee
        wrote on last edited by
        #8

        Sounds like a case of the wrong tool for the job. Implicit enums are great for representing a fixed set of ideas/behavior (like flags) that isn't mapped to specific values. If you need a fixed set mapped to specific values use explicit enums :doh:

        D 1 Reply Last reply
        0
        • J Jon McKee

          Sounds like a case of the wrong tool for the job. Implicit enums are great for representing a fixed set of ideas/behavior (like flags) that isn't mapped to specific values. If you need a fixed set mapped to specific values use explicit enums :doh:

          D Offline
          D Offline
          den2k88
          wrote on last edited by
          #9

          Precisely. Even if I'm used to always explicit the enums - mainly because I work with a system that has a lot of marshalling and communications between different paradigms so more often than not I end up having to fix those values sooner or later.

          * CALL APOGEE, SAY AARDWOLF * GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X * Never pay more than 20 bucks for a computer game. * I'm a puny punmaker.

          1 Reply Last reply
          0
          • P Pete OHanlon

            Sooooo, when I pass in TRalala to his code, it's still going to be superior. :rolleyes:

            This space for rent

            B Offline
            B Offline
            Bruce Patin
            wrote on last edited by
            #10

            You're going to get 0, unless you mean "Tralala".

            1 Reply Last reply
            0
            • B Bernhard Hiller

              Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:

              private int DecodeElementType(string elementName)
              {
              switch (elementName)
              {
              case "Blah": return 1;
              case "Blub": return 2;
              case "Hmpf": return 3;
              case "Grml": return 4;
              case "Tralala": return 5;
              default: return 0; // unknown
              }
              }

              I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...

              L Offline
              L Offline
              Leng Vang
              wrote on last edited by
              #11

              My guess, he didn't know. Self-tough programming. What so superior about coding manually. The point of using enumeration is to allow the compiler to check for potential human errors. Tell him to go back to school.

              L 1 Reply Last reply
              0
              • L Leng Vang

                My guess, he didn't know. Self-tough programming. What so superior about coding manually. The point of using enumeration is to allow the compiler to check for potential human errors. Tell him to go back to school.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #12

                I'd guess he did learn to code in school. Meaning he had coding courses, learned nothing (as usual, because those courses invariable suck), and then assumed he would not need learn any further.

                1 Reply Last reply
                0
                • B Bernhard Hiller

                  Once upon a time, the concept of enumeration had not yet been invented. Instead, you had to use long lists of const definitions mapping some name to some int. So I found a colleague writing code like:

                  private int DecodeElementType(string elementName)
                  {
                  switch (elementName)
                  {
                  case "Blah": return 1;
                  case "Blub": return 2;
                  case "Hmpf": return 3;
                  case "Grml": return 4;
                  case "Tralala": return 5;
                  default: return 0; // unknown
                  }
                  }

                  I asked him if he knew what an enum is, and how to use Enum.Parse / Enum.TryParse. He said he knew that. But his code is superior...

                  M Offline
                  M Offline
                  Munchies_Matt
                  wrote on last edited by
                  #13

                  Whats the language that can use a string in a switch statement?

                  B 1 Reply Last reply
                  0
                  • M Munchies_Matt

                    Whats the language that can use a string in a switch statement?

                    B Offline
                    B Offline
                    Brisingr Aerowing
                    wrote on last edited by
                    #14

                    I think the snippet is in C#, which does allow that.

                    What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

                    M 1 Reply Last reply
                    0
                    • B Brisingr Aerowing

                      I think the snippet is in C#, which does allow that.

                      What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

                      M Offline
                      M Offline
                      Munchies_Matt
                      wrote on last edited by
                      #15

                      OK, makes sense. C++ allows for a lot of this kind of thing through operator overloading, though I tend to be a pure C programmer, and I like the level of clunky direct control you have of the code with C. :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups