avatarDev Leader

Summary

The article discusses common misuse of enums in C# programming, emphasizing their proper application for finite, unchanging sets of values and cautioning against overuse in dynamic contexts.

Abstract

The article "You’re Using Enums Wrong," featured in the Dev Leader Weekly newsletter, provides a brief primer on enums in C# for those unfamiliar with the concept. It explains how enums blend numeric values with readable names, making code more maintainable than using magic numbers or strings. However, the author argues that developers often misuse enums by applying them to data that is not finite or subject to frequent changes, leading to increased maintenance efforts. The article suggests that enums should be reserved for fixed sets of values, such as days of the week, and advises against using enums where values change often or are serialized to databases, as this can cause issues with existing data. The author recommends limiting the number of places where enums are consumed to reduce maintenance complexity.

Opinions

  • Enums are praised for enhancing code readability and maintainability when used correctly.
  • The author believes that enums are misused when they represent data that is not fixed, leading to maintenance challenges.
  • Frequent changes to enum values can create significant maintenance overhead due to the need to update all consum

You’re Using Enums Wrong

A Dev Leader Weekly Exclusive Article

This is a quick article that was exclusive to my newsletter subscribers. It was a part of one of my November newsletters — only available in the newsletter archive now.

A Quick Primer On Enums

If you’re not familiar with Enums in C#, we should probably get that cleared up before diving any deeper! Enums allow us to create a type in C# that feels like a blend of strings and integers. Enums hold a numeric value, but they also have a name associated with them. Here’s a brief example:

public enum WeekDays
{
    Monday, // this has a value of 0
    Tuesday, // this has a value of 1
    Wednesday, // this has a value of 2
    Thursday, // this has a value of 3
    Friday, // this has a value of 4
}

This allows us to write code where we can access WeekDays.Thursday and work with effectively a numeric value with a readable name for developers. This is more practical than using the string “Thursday” everywhere because the Enum is essentially a numeric value. But this Enum is also more expressive than just a constant numeric value! Seems like a win!

Before we touch on some more advanced topics, here’s a video on getting started with enums:

Why You’re Using Enums Wrong

To be clear — while the title of this piece is a bit click-baity, I’m wagering at least most people fit into this group. And hopefully you know me by now — I don’t believe in absolutes. So “wrong” is too strong of a word here, but I am going to explain why I think you might be making more work for yourself.

Enums are super convenient. As C# programmers, they give us a big boost to readable and understandable code because they’re expressive. Instead of comparing numbers slapped into some constants — or better yet, just left as magic numbers maybe with a comment or two as the cherry on top — Enums allow us, as developers, to know the meaning. They’re readable. They have names. It’s great stuff.

Where we start to misuse them is representing data that is not finite or fixed. And when you couple that with code that is consuming or working with the Enums in multiple places… here comes your fun time with maintenance.

If your set of values in your Enum is changing frequently, multiply your maintenance cost by all of the spots that are consuming those Enum values. All of those if/else or switch statements that are operating on the Enum ranges? Yeah, those need to be maintained now. And even scarier — You’re serializing the enum values out to some persistent storage like a database… Guess what? Someone just inserted a new value into your range so “3” is no longer Products.NicksCoolProduct (or someone removes an enum value so “NicksCoolProduct” can’t be parsed from your DB record).

The Path Forward

So here’s the deal. It’s not “wrong” to use Enums this way, it’s just that you’re making work for yourself. I highly recommend that you use Enums for finite values (days of the week, months of the year, etc…) or for sets of values that will change very infrequently (maybe a finite state machine with well-understood behavior/states). You can also aim to push Enum consumption into fewer spots so you’re reducing that maintenance multiplier I mentioned above.

In summary — there’s no right or wrong here. But if you consider these tips, then I think you can make it easier on yourself to maintain codebases with Enums. And if you watch this video on enum usage, I hope that it’ll help explain this clearly:

Want More Dev Leader Content?

  • Follow along on this platform if you haven’t already!
  • Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos: SUBSCRIBE FOR FREE
  • Looking for courses? Check out my offerings: VIEW COURSES
  • Watch hundreds of full-length videos on my YouTube channel: VISIT CHANNEL
  • Visit my website for hundreds of articles on various software engineering topics (including code snippets): VISIT WEBSITE
  • Check out the repository with many code examples from my articles and videos on GitHub: VIEW REPOSITORY
Csharp
Dotnet
Coding
Programming
Developer
Recommended from ReadMedium