C #: Enum anti-patterns

There was talk that Enums generally violate the principles of clean code, so I'm looking for people who love Enum anti-patterns and alternative solutions for them.

For example, I saw this code:

switch(enumValue) { case myEnum.Value1: // ... break; case myEnum.Value2: // ... break; } 

This is one step better than switch-statements with magic strings, but this could probably be better solved with a factory, container, or other template.

Or even the old school code:

 if(enumValue == myEnum.Value1) { // ... } else if (enumValue == myEnum.Value2) { // ... } 

What other anti-patterns and more effective implementations have you experienced with enumerations ?

+5
enums c # design-patterns anti-patterns
Oct 12 '10 at
source share
4 answers

I think Enums are quite useful. I wrote several extensions for Enum that added even more value to use

Firstly, there is a method for expanding the description

 public static class EnumExtensions { public static string Description(this Enum value) { var entries = value.ToString().Split(ENUM_SEPERATOR_CHARACTER); var description = new string[entries.Length]; for (var i = 0; i < entries.Length; i++) { var fieldInfo = value.GetType().GetField(entries[i].Trim()); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); description[i] = (attributes.Length > 0) ? attributes[0].Description : entries[i].Trim(); } return String.Join(", ", description); } private const char ENUM_SEPERATOR_CHARACTER = ','; } 

This will allow me to define en enum as follows:

  public enum MeasurementUnitType { [Description("px")] Pixels = 0, [Description("em")] Em = 1, [Description("%")] Percent = 2, [Description("pt")] Points = 3 } 

And get the label by doing the following: var myLabel = rectangle.widthunit.Description() (eliminating the need for a switch ).

This will return "px" if rectangle.widthunit = MeasurementUnitType.Pixels or it will return "px, em" if rectangle.widthunit = MeasurementUnitType.Pixels | MeasurementUnitType.Em rectangle.widthunit = MeasurementUnitType.Pixels | MeasurementUnitType.Em .

Then there is

  public static IEnumerable<int> GetIntBasedEnumMembers(Type @enum) { foreach (FieldInfo fi in @enum.GetFields(BindingFlags.Public | BindingFlags.Static)) yield return (int)fi.GetRawConstantValue(); } 

Which will allow me to go through any enum with int-based values ​​and return the int values ​​themselves.

I find that they are very useful in the helpful allready concept.

+11
Oct 12 2018-10-12
source share

I see the presence of two operators as a design symptom without OO as explained later in this answer .

+1
Oct 12 2018-10-10
source share

This is not an answer, nor is it a contribution to the Enum anti-pattern list.

While reviewing the code this morning, I came across a case similar to the following: all in one class.

Two cases:

  • Before use
  • After drinking

..

  public enum ListEnum { CategoryOne, CategoryTwo, CategoryThree, CategoryFour } public class UIELementType { public const string FactoryDomain = "FactoryDomain"; public const string Attributes = "Attributes"; } 
+1
Jul 12 '14 at
source share

Using enumerations in a non-anti-pattern. Some refactoring books use this code to demonstrate how to replace it with polymorphism. It would be nice if you abuse enumerations in the code.

0
Oct 12 '10 at 10:22
source share



All Articles