← Back to context

Comment by wvenable

5 years ago

> case KIND_15: return parseObjectOfKind5();

I don't like this type of code for exactly this reason.

The association between the two has to be written somewhere. Can you provide an alternative approach that isn't vulnerable to typos?

  • Their point seems to be in the mismatch of names. Imagine seeing this sequence, with names instead of numbers:

      switch(HTTP_METHOD) {
        case PUT: processPut(...);
        case POST: processPost(...);
        case GET: processDelete(...); // wtf
        case DELETE: processGet(...); // mate?
      }
    

    To the reader that appears to be an error even if it is precisely the thing you want to happen.

  • This is basic DRY violation. The number is duplicated twice and that can get out of sync (as in the example).

    I think everyone has coded this way out of expedience and some of us have eventually messed it up too. But you could, for example, use a macro in C to only list the number once. It might not be a trade-off worth making though.

    Personally, I've used reflection to do this kind of mapping of enums to functions in a language that supports reflection.

    • The code was inspired by a piece of Java code that rendered geometric primitives. It was basically case "box": return parseBox(); as you suggested in your other post.

      Turning "box" into "parseBox" using string operations and then using reflection to call the right method is an approach I'd consider in Ruby, but not in Java. It breaks IDE features like "Find Usages" and static analysis, and the code to dynamically invoke a Java method is more annoying to review than the boring repetition in my posted snippet.

    • > This is basic DRY violation. The number is duplicated twice 2 and that can get out of sync (as in the example).

      I think it's pretty clear that the number is a placeholder for something reasonable, e.g. making an association between two distinct sets of concepts. You'll still be vulnerable to copy-paste or typo issues.

      > Personally, I've used reflection

      Now you have two problems (and still have to maintain an association between two sets of concepts).

      1 reply →

  • Depends on your language, but you could use the type system instead; e.g. the function is chosen by the compiler based on the type of the value.

    In more dynamic languages, you could probably use introspection.

    Lastly, this does not alleviate the association issue, but I prefer the alternative of declaring the associations in an array/map somewhere, and using map[enum_value]() instead of the switch.

    • In a lot of ways, I think the map solution is actually worse. There isn't an easy way to figure out what code is used for a given enum value with the map alone. The searchability depends on how the map is initialized. Not to mention a map will always introduce null or option types that you have to handle.

      A map can be a good solution though, particularly if it's something like mapping enum values to strings that is constructed via EnumClass.values().map... or something so that you know the map is a total function.

      4 replies →

I mean, hilarious point, but I'm not sure it's valid. Clearly he's numbering things here, but I am guessing in the real world you're far likelier to see tests of typing or some not-numerically-indexed value rather than literally numbered ENUMs

  • This can happen with any enum to function mapping. You cut and paste and forget to change one or the other.

    In C, you could use a macro in this call to make sure that the name/number is only specified once.

    • I mean, this can happen with any code by that logic if you're cut and pasting.

      I think the real issue is types aren't included in the example. I work in much higher-level languages, but if you are passing strongly typed objects thru and your switch is derived on this typing, it's probably going to be illegal in your type system to return certain results.

      If your type system doesn't validate your results, then you'll be prone to the class of error you are discussing. Maybe that's common in C.