• foo (unregistered)

    Major oversight: They forgot FILE_NOT_FOUND.

  • Hmpfs (unregistered)

    Maybe they were old Java coders. Something like this used to be quite a usual pattern in older versions of that language, since it did not have enums.

  • Eric (unregistered)

    It's called the type safe enum design pattern, and it does have its uses. They're rare, but they do exist. Captcha was enim. :)

  • (cs) in reply to Eric
    Eric:
    It's called the type safe enum design pattern, and it does have its uses. They're rare, but they do exist. Captcha was enim. :)

    type safe? in java enums are their own class unlike in other C style languages where they are int constants

  • WailyDTF (unregistered)

    This is not necessarily a WTF.

  • (cs)

    Am I seeing a potential problem, or is this just my C++ coding knowledge getting in the way of C# understanding? There does not seem to be a void constructor for the class nor its parent. If someone tries to instantiate an object for this class, you should get some compile errors, saying something about no available constructor taking 0 arguments.

  • WailyDTF (unregistered) in reply to Nutster
    Nutster:
    Am I seeing a potential problem, or is this just my C++ coding knowledge getting in the way of C# understanding? There does not seem to be a void constructor for the class nor its parent. If someone tries to instantiate an object for this class, you should get some compile errors, saying something about no available constructor taking 0 arguments.
    Actually, the code snippets as provided won't compile themselves, since the constructors have no body defined.

    Usually in these kinds of type-safe enumerations, one constructs all the possible static "enumeration values" within the private constructor.

  • Kabili (unregistered) in reply to Nutster

    Most new languages don't require a constructor with zero arguments.

  • haha (unregistered)

    As provided it won't work I think, but I think they tried something like this: https://stackoverflow.com/questions/3007596/behaviour-to-simulate-an-enum-implementing-an-interface/3007754#3007754

    Which can be really usefull so you won't have to generate giant switches everywhere in the code using the "enum", but you can keep specific values, formatting with the "enum" it belongs with.

  • Anonymoose (unregistered)

    These are cool. They can work as enums, but you can also add more properties to it, so you can basically have a coding identifier, a numerical ID, and a string. Like { BOOL_FILENOTFOUND, 666, "File not found! Crikey!" }

  • snoofle (unregistered)

    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

  • faoileag (unregistered) in reply to haha
    haha:
    As provided it won't work I think, but I think they tried something like this: https://stackoverflow.com/questions/3007596/behaviour-to-simulate-an-enum-implementing-an-interface/3007754#3007754
    There is a full blog entry covering the pattern here.

    Including the why, the how and the advantages.

  • Bob Lied (unregistered)

    If you were coding Java about a decade ago, before the language magically sprouted its enumeration feature, this was not only not a WTF, it was a best practice. It's item 21 in Joshua Bloch's "Effective Java" book, which was practically required reading.

  • yojin (unregistered) in reply to Bob Lied

    I think this is the real wtf. Only in Java would you need to create two classes to use an enum. In Java this is not insane, and in fact is a documented best practice.

  • Lucy Fur (unregistered) in reply to Anonymoose
    Anonymoose:
    These are cool. They can work as enums, but you can also add more properties to it, so you can basically have a coding identifier, a numerical ID, and a string. Like { BOOL_FILENOTFOUND, 666, "File not found! Crikey!" }

    +666, er, I mean +1

  • (cs) in reply to yojin
    yojin:
    I think this is the real wtf. Only in Java would you need to create two classes to use an enum. In Java this was not insane, and in fact was a documented best practice.
    FTFY; Java has had proper enums since 5…
  • (cs) in reply to Nutster
    Nutster:
    Am I seeing a potential problem, or is this just my C++ coding knowledge getting in the way of C# understanding? There does not seem to be a void constructor for the class nor its parent. If someone tries to instantiate an object for this class, you should get some compile errors, saying something about no available constructor taking 0 arguments.
    In C#, if no constructors are provided, the compiler injects a void constructor with no body for you.
  • Evan (unregistered) in reply to Hmpfs
    Hmpfs:
    Maybe they were old Java coders.
    Hell, maybe it was old code.
  • Nagesh (unregistered)

    people who learned java in early days had to roll out their own Enums.

  • Steve (unregistered)

    This is similar to Jimmy Bogard's solution here: http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/

    By modeling the enumeration as a class structure, it allows you to map it to a table using your ORM, thus keeping your lookup tables in sync with your code, while at the same time being flexible enough to allow additional codes to be in the database and not break your app.(perhaps an older version of an API)

    I haven't used the new enum support in Entity Framework, but from reading the documentation I don't see anything that implies it works the same way.

    There are a lot of WTF's in this world. This is not one of them.

  • Xaser (unregistered)

    Oh god... I'm doing the thing. I'M DOING THE THING.

    inhales deeply

    This is not a WTF.

    The Los Techies article linked by faoileag makes a good case for a full-blown enum replacement, but judging from the presence of a single string parameter in the constructor, today's featured code looks more like it's trying to implement a 'String Enum', which is not "rolling your own" but rather adding something the language itself doesn't have at all (assuming the code is indeed C#). It's there to solve a problem that vanilla enums can't.

    Granted, the actual code featured in the article doesn't actually work, but it looks to me like it's incomplete (probably truncated during anonymization or submission). It won't compile as-is (verified by self-testing, again assuming it's C#) and it's not actually creating any instances, making the whole thing moot. Concept still stands as a non-WTF though, unless I guess one subscribes to a "don't ever use string 'enums'" point of view. :P

    exhales

    I did the thing.

  • Evan (unregistered) in reply to Evan
    Evan:
    Hmpfs:
    Maybe they were old Java coders.
    Hell, maybe it was old code.
    Actually I take this back, because as others have pointed out, it is c#. Whoops. Not paying enough attention.
  • (cs)

    Actually, I was surprised they didn't roll their own "string", "char", and arrays as well. I guess they have some restraint.

    It's clear they need more restraint yet, though...maybe a straitjacket?

  • Snoffle (unregistered) in reply to snoofle

    Hear Hear! Discourse is TRWTF

  • OldCoder (unregistered) in reply to snoofle
    snoofle:
    Please stop using discourse!
    Fixed that for you.

    Discourse is so bad that if it continues, I'll be forced to abandon this site after coming regularly for ~14 years.

  • (cs) in reply to snoofle
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

  • Nagesh (unregistered) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    Haven't you heard of USB sticks and Portable Apps?

  • v (unregistered)

    Ok, it is nice to read from the above comment that this is not as much a WTF as the WTF author thought... but WTF is up with the array part in it?

    public static readonly SubscriptionStatus[] STATUSES;

    WTF?

  • v (unregistered)

    Oh nevermind, I get it now, we are only shown like the header file and not the implementation, where that array and all other members for that matter would have a more sensible definition and not be completely uninitialized.

    So the full code makes perfect sense, and the WTF is with the posted cutout.

  • Andrew (unregistered)

    Way better than FILE_NOT_FOUND.

  • Ehnonymouse (unregistered) in reply to Nagesh
    Nagesh:
    The Great Lobachevsky:
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    Haven't you heard of USB sticks and Portable Apps?

    Use of USB sticks is incompatible with a secure environment. As is any version of IE greater than 8. The only reason we're not still using Windows XP is because it cost too much to pay for the off-support support. Remember the IT professional's creed: "It's not secure if users can still use it."

  • (cs) in reply to Ehnonymouse
    Ehnonymouse:
    Nagesh:
    The Great Lobachevsky:
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    Haven't you heard of USB sticks and Portable Apps?

    Use of USB sticks is incompatible with a secure environment. As is any version of IE greater than 8. The only reason we're not still using Windows XP is because it cost too much to pay for the off-support support. Remember the IT professional's creed: "It's not secure if users can still use it."

    I second this. Nagesh has obviously never worked in a locked-down corporate environment, where you can't download, open a zip file, install anything, use USB, or run an app off of a portable device.

  • G. Applqst (unregistered)

    This class approach can give rise to inheritance hierarchies of enumerations.

  • (cs) in reply to Bob Lied
    Bob Lied:
    If you were coding Java about a decade ago, before the language magically sprouted its enumeration feature, this was not only *not* a WTF, it was a best practice. It's item 21 in Joshua Bloch's "Effective Java" book, which was practically required reading.
    I myself wrote a considerable proportion of a majorly colossal app using a pattern derived from this. It was the way you did it in those days.

    You also got to point out that the design of the Java 5 Enum relied a lot on this thesis of Bloch's. Come on, it wasn't that long ago that Java 4 was the only game in town.

    The advantage to this pattern is that it's straightforward to replace it with the equivalent (much more compact) Java 5 version when you upgrade, and (if you done it right) it's practically plug-and-play.

    Conclusion: Not a WTF.

  • (cs)

    I'm still amazed at how bad most developers are when it comes to enumerations.

  • (cs)

    I like how Haskell handles this, with its algebraic data types. All you have to do is create a data type with alternative nullary constructors:

    data Color = Red | Orange | Yellow | Green | Blue | Indigo | Violet
    

    Want to "apply" a color to a string?

    data Colored a = Colored Color a deriving (Functor)
    
    redString :: String -> Colored String
    redString = Colored Red
    

    Want to be more general?

    red :: a -> Colored a
    red = Colored Red
    

    Oh look, Colored is an adjunction, and red is in the Kliesli algebra for its monad. So functions like red form an "imperative" dsl for coloring things.

  • John Muller (unregistered) in reply to Evan
    Evan:
    Evan:
    Hmpfs:
    Maybe they were old Java coders.
    Hell, maybe it was old code.
    Actually I take this back, because as others have pointed out, it is c#. Whoops. Not paying enough attention.

    I've been doing C# for 13 years, I would call some of that code 'old' (but not 'ancient' like COBOL/C)

  • Doctor_of_Ineptitude (unregistered) in reply to OldCoder
    OldCoder:
    snoofle:
    Please stop using discourse!
    Fixed that for you.

    Discourse is so bad that if it continues, I'll be forced to abandon this site after coming regularly for ~14 years.

    Alex seems to be missing in action, and his minions seem happy to burn the place down. Discourse shows the number of comments for articles at the bottom. Just have a look as to how many comments are there as compared to (pick at random) any other article. Fact is people have started fleeing the ship. And those in power don't care. Discourse has features that don't matter, and fails miserable at it's basic task, making comments readable and enabling a conversation. This is a WTF of epic proportions right at thedailywtf.

  • (cs) in reply to Doctor_of_Ineptitude
    Doctor_of_Ineptitude:
    fails miserable at it's basic task

    hah

  • Doctor_of_Ineptitude (unregistered) in reply to chubertdev
    chubertdev:
    Doctor_of_Ineptitude:
    fails miserable at it's basic task

    hah

    I do have a doctorate in ineptitude. They have no such excuse.

  • (cs) in reply to Captain Oblivious
    Captain Oblivious:
    I like how Haskell handles this, with its algebraic data types. All you have to do is create a data type with alternative nullary constructors:

    ((snippage))

    Want to be more general?

    red :: a -> Colored a
    red = Colored Red
    

    Oh look, Colored is an adjunction, and red is in the Kliesli algebra for its monad. So functions like red form an "imperative" dsl for coloring things.

    So to use Haskell, I need to understand adjuntions, monads, and... Kliesli algebra? Yow. Thank you, C# and Javascript (and a dozen other things) for me.

  • (cs)

    Hi Guys,

    I too, used to roll my own enums and type safe collections before they were built into java 1.5, and have no doubt that folks did the same in other languages.

    I omitted it from the post, but this particular feat of engineering dates significantly after enumerations were added to the language...

  • Friedrice The Great (unregistered) in reply to Nagesh
    Nagesh:
    The Great Lobachevsky:
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    Haven't you heard of USB sticks and Portable Apps?
    At my office, plugging a USB stick into a computer gives one of two results. If the stick is writable, the security software wipes it. If the stick isn't writable, the security software blocks access to it. But Discourse is still TRWTF.

  • Neal Blomfield (unregistered)

    Depending on the language, rolling your own enums using classes is actually better than using enums as you can add behaviour to class based enums but you can't add behaviour to enum based enums (at least in c# you can't). If you start with a class based enum it takes a lot more work to replace it later when you want to add behaviours.

  • Neal Blomfield (unregistered) in reply to Neal Blomfield
    Neal Blomfield:
    Depending on the language, rolling your own enums using classes is actually better than using enums as you can add behaviour to class based enums but you can't add behaviour to enum based enums (at least in c# you can't). If you start with a class based enum it takes a lot more work to replace it later when you want to add behaviours.
    ... If you start with *enum-based* enums ...
  • dreadwolf (unregistered) in reply to yojin
    yojin:
    I think this is the real wtf. Only in Java would you need to create two classes to use an enum. In Java this is not insane, and in fact is a documented best practice.
    Except this isn't Java and Java has had enums for 10 years. Why did your mommy let you near a computer?
  • (cs) in reply to D-Coder
    D-Coder:
    Captain Oblivious:
    I like how Haskell handles this, with its algebraic data types. All you have to do is create a data type with alternative nullary constructors:

    ((snippage))

    Want to be more general?

    red :: a -> Colored a
    red = Colored Red
    

    Oh look, Colored is an adjunction, and red is in the Kliesli algebra for its monad. So functions like red form an "imperative" dsl for coloring things.

    So to use Haskell, I need to understand adjuntions, monads, and... Kliesli algebra? Yow. Thank you, C# and Javascript (and a dozen other things) for me.

    The Kliesli algebra for a monad M is just the the set of functions with the type a -> M a -- the ones that let you compose monad actions.

    Your objection comes down to "math is hard wah" when it is demonstrably easier than the ad hoc constructions a typical language uses.

    How would you make a similar DSL (one that lets you attach colors to arbitrary objects) in the OO language of your choice?

  • Yuri (unregistered) in reply to Friedrice The Great
    Friedrice The Great:
    Nagesh:
    The Great Lobachevsky:
    snoofle:
    Please stop using discourse! How else can I impersonate snoofle in order to say something he'd thought is witty if he were Me?

    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    Haven't you heard of USB sticks and Portable Apps?
    At my office, plugging a USB stick into a computer gives one of two results. If the stick is writable, the security software wipes it. If the stick isn't writable, the security software blocks access to it. But Discourse is still TRWTF.
    Our IT security take the "All your USB is belong to us" type attitude....

    If it touches a work system it become there's

  • Steve (unregistered) in reply to The Great Lobachevsky
    The Great Lobachevsky:
    My employer only supplies IE8, so I can't even read the comments when Discourse is in use.

    The WTF is why you still work for them.

  • Norman Diamond (unregistered) in reply to Friedrice The Great
    Friedrice The Great:
    At my office, plugging a USB stick into a computer gives one of two results. If the stick is writable, the security software wipes it. If the stick isn't writable, the security software blocks access to it.
    Ah, that explains it. Lois Lerner's e-mail was backed up on a USB stick, along with records of my Forms 1099 and some registered letters.

Leave a comment on “Rolling Your Own”

Log In or post as a guest

Replying to comment #435890:

« Return to Article