Breaking Changes: .Net Enums an often overlooked dangerzone

Hierarchical Testing – When Can I Test It?
November 25, 2017
The discussion of complex increases in complexity….
December 23, 2017

Breaking Changes: .Net Enums an often overlooked dangerzone

It has been a while since there was a post in this series, but just today I was involved in a set of discussions related to adding new values to an already published (and widely consumed) enum; so I figured it was a good topic for a posting.

To start, lets consider a very simple enum:

enum ImportantLetters { a, b ,h, i}

At some point after publication, “c” becomes an ImportantLetter, so there is a desire to add it to the declared enum. The most natural place would be to add it between “b” and “h” giving:

     enum ImportantLetters { a, b ,c, h, i}

Seems safe enough…. But what if someone had previously written

     static int Ordinal(ImportantLetters x)
     {
        List<ImportantLetters> set = new List<ImportantLetters>();
        foreach (ImportantLetters s in Enum.GetValues(typeof(ImportantLetters)))
           set.Add(s);
        return set.IndexOf(x);
     }

Existing code which returned a value of 2 when passed “h” will now return 3 !!!!!    Any decisions or calculations which were based on the return value will now be wrong.

Unfortunately, with the .Net implementation of enum’s there is no way to avoid this. Some options might be viable if enums supported inheritance along with operators and co/contra variance. Alas, they do not.

The root of the problem is that the initial set of values was not an immutable (never changing) set of values. It would require a different implementation (rather than enums) to allow for proper representation of a set of values with the ability to safely add new values without the potential for breaking code.

Of course there is always be the need to make breaking chances, despite one’s desire to adhere to SOLID (and other) design and implementation principles. But we are fallible, and there will be occurrences. The key element is to recognize them, and deal with them accordingly. If one is making an effort to follow Semantic Versioning [SemVer], then a breaking change requires an increment in the major version number.

11 Comments

  1. StephenSet says:

    viagra cialis viagra for sale online viagra

  2. Georgedot says:

    viagra prices buy viagra generic buy viagra online canada

  3. Georgedot says:

    viagra price online viagra prescription viagra cheap

  4. Jeremyedits says:

    cialis at a discount price buy cialis online how much does cialis cost

  5. EdwardBoall says:

    buy viagra online cheap cheap viagra buy generic 100mg viagra online

  6. Samuelpek says:

    medicine for ed vitality ed pills sexual dysfunction in men

  7. Rogerinhef says:

    can you buy viagra over the counter cheap viagra canadian viagra
    top erection pills

  8. If you are pregnant or breast-feeding, ask a health professional before use.
    difference between generic viagra and viagra otc generic viagra

  9. Rogerinhef says:

    canada viagra buy generic drugs how much will generic viagra cost
    ambien without a doctor’s prescription

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE CONSULTATION
Loading...