Jordan Benge
2 min readJun 26, 2024

--

I’m not the author, but I’ve seen a lot of hate for Enums over the years (some valid, some not). So this is just my 2c. I’m also only referring to String Enums, and not Numeric ones.

Here are some of the complaints I’ve heard about Enums in general:

1. increases compiled code: valid if you’re not using `const Enums`.

2. Not a native feature: valid

3. Increased code complexity: only really valid if you use them poorly or use them for simple cases where a string literal would suffice.

4. More verbose: Never a problem in my world. I want more verbosity tbh.

5. Less flexible when you need to extend or modify them dynamically: You’re using them to solve the wrong thing.

Despite all of that though, I think that string Enums are valuable to have. For the following reasons:

1. Verbosity: I can be as verbose as I want to be with my Enum index naming. With string literal types if I want a really long and descriptive name for a type, I have to pull it out into it’s own sub type, which isn’t terrible if you’re only doing it once or twice, but once you get more and more literal values, it becomes beneficial to be able to “encode” business logic into a simple string Enum, and just create a namespace grouping instead.

2. Autocomplete: is pretty similar for both string literals and string Enums, but I’ve found that more IDEs have better support for Enums than string literals. Anecdotal, but still.

3. Debugging / refactoring support: With a Enum, I can find all usages easily, without having to interpret code (no matter how quickly that can be done). This means that I can add & remove values quicker as I’m not searching for a particular string comparison anymore, I’m just comparing to the Enum values.

4. Type safety: String Enums are a more “type safe”, option because you cannot assign values outside of the defined Enum to a variable of the Enum type. String literals do this as well, but they are less strict in the long run, and more easily circumvented in my opinion.

5. Template Readability: I think it makes the code more readable, as you’re doing comparisons in the templates. You’re not comparing to this “magic” string literal value, you’re comparing to an Enum value. The trade off with that, is that you have to then define a variable in your component dedicated to that Enum, but that’s simple and much more elegant to understand what’s going on at a glance in my opinion.

So yes, the overhead is a big tradeoff, the benefits of better, cleaner and faster development makes up for it in my opinion. I’ve yet to run into a situation where I needed to optimize my code by transforming Enums into string literal types. There are simply way more useful optimizations to do before that becomes a concern in my opinion.

--

--

Jordan Benge
Jordan Benge

Written by Jordan Benge

My name is Jordan Benge, I’m a freelance developer, who sometimes likes to write helpful articles on Medium for people who want to do things but don’t know how.

No responses yet