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
viagra cialis viagra for sale online viagra
viagra prices buy viagra generic buy viagra online canada
Please credit the National Cancer Institute as the source.
how long does it take for a 100mg viagra to kick in? What is viagra
viagra price online viagra prescription viagra cheap
cialis at a discount price buy cialis online how much does cialis cost
buy viagra online cheap cheap viagra buy generic 100mg viagra online
medicine for ed vitality ed pills sexual dysfunction in men
can you buy viagra over the counter cheap viagra canadian viagra
top erection pills
If you are pregnant or breast-feeding, ask a health professional before use.
difference between generic viagra and viagra otc generic viagra
canada viagra buy generic drugs how much will generic viagra cost
ambien without a doctor’s prescription
zovirax dosage for fever blister https://www.herpessymptomsinmen.org/productacyclovir/