Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3225640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:21:46+00:00 2026-05-17T16:21:46+00:00

There has been talk of Enums in general violating Clean Code-principles, so I’m looking

  • 0

There has been talk of Enums in general violating Clean Code-principles, so I’m looking for people’s favorite Enum anti-patterns and alternative solutions for these.

For example I’ve seen code like this:

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

It’s one step better than switch-statements with magic strings, but this probably could have been solved better with a factory, a container or other pattern.

Or even old-school code like this:

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

What other anti-patterns and better implementations have you experienced with enums?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:21:47+00:00Added an answer on May 17, 2026 at 4:21 pm

    I think Enums are quite useful. I’ve written a few extensions for Enum that have added even more value to its use

    First, there’s the Description extension method

    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 like this:

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

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

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

    Then, there is a

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

    Which will let me traverse any enum with int based values and return the int values themselves.

    I find these to be very useful in an allready useful concept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There has been a lot of talk around iPad-Apps / Approval / Store-related Questions.
There has been a lot of talk about contra-revolutionary NoSQL databases like Cassandra ,
There has been some talk of Website performance monitoring tools and services on stackoverflow,
I know there has been previous talk on here about screencasting tools/apps, however I
There has been similar questions asked (and answered), but never really together, and I
There has been a flaw on my design for most of .xml files where
I'am aware there has been a generic question about a best IDE in C++
In the Zend Framework Quickstart , there has been a change from models that
There has always been a confusion with preg_match in php. I have a string
Has there been discussion around how to resolve equivalent openids? Meaning, I personally have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.