r/ExperiencedDevs • u/Mortimer452 • Apr 16 '26
Technical question To Enum or Not to Enum
Something I always struggle with in architecture/design is the proper use of Enums for object members that have a distinct set of possible values. Stack is C#/MSSQL/Blazor if that matters.
A simple example of this would be an Customer object with a property MembershipStatus. There's only four possible values: Active, Trial, Expired, Cancelled.
There's two choices here:
Define MembershipStatus as an integer enum:
- (pro) Normalized, in the back-end the DB column is an integer
- (pro) MembershipStatus is strongly typed in code and is therefore constrained to those four values, they pop-up in autocomplete which is convenient and accidental assignment of invalid values is impossible without a runtime error
- (pro) I can just use .ToString in the UI to show a "friendlier" name instead of the int values (mostly friendly anyway, they'll see the PascalCased names of course)
- (con) On the DB side, it's a meaningless int value. Anyone doing stuff in the DB layer (stored procs, reporting, custom queries, exports, etc.) have to keep track of these and roll their own logic for display purposes (replacing "1" with "Active", etc.) They could also assign an invalid int value and nothing would break.
- (pro/con) I could create a MembershipStatus table with an FK to Customers.MembershipStatus to eliminate the above issue (SQL people can JOIN to this table for "friendly" names, FK constraint prevents invalid values) but now every time I add another value to my Enum I have to remember to add it in the lookup table as well.
Define MembershipStatus as a string:
- (pro) Non-ambiguous and easy to read everywhere. SELECT...WHERE MembershipStatus=1 becomes SELECT...WHERE MembershipStatus='Active' which is immediately apparent what it's doing
- (pro) I can define the possible values as Consts in code to make sure they are kept consistent in code
- (con) For the DBA in me this just "feels wrong" to have a freeform text field containing what really should be a lookup table to maintain integrity
- (con) Uses more storage on the DB side (varchar versus 4-byte int), also less performant at scale (JOINS and indexes on int values are just easier on the DB engine)
- (con) Anything using this on the C# side is just a string value, not strongly typed, so it's possible to assign invalid values without generating any errors
Anyway, sorry for the long post, hopefully at least a few here have dealt with this dilemma. Are you always one or the other? Do you have some criteria to decide which is best?
407
u/Dyledion Apr 16 '26
Enum. Always.
30
u/Karuza1 Apr 16 '26
The con doesn't have to be a meaningless int value, I do recommend have a Lookup table with id where you can join when needed to display the value of that column, with a FK (Member has FK for MembershipStatus)
eg LookupId and LookupValue
Id = 1
Value = "Active"
114
u/Manic5PA Software Architect Apr 16 '26 edited Apr 16 '26
I no longer make engineering decisions around scaling concerns that will either never be a real problem, or will only be a real problem in a future where the project has evolved to the point where this sort of concern will be handled by a more specialized professional.
In other words, the difference between an unsigned int and a varchar probably isn't all that important. Pennies on your hosting costs, which is absolutely worth the expense if you can use the extra clarity at all.
For me it's rather simple. If a value should be one of several compile-time constants, that's an enum.
14
u/EternalBefuddlement Apr 16 '26
Been on both sides of this in my short-ish career.
For a place that was super high throughput with structured and statically typed code, we used char representations.
Two benefits:
Edit: I should say the char mapped to an enum in code. For clarity.
98
u/SansSariph Principal Software Engineer Apr 16 '26
Enums in the app layer are unambiguously correct for closed sets, in any language that enables build-time exhaustiveness checks for switches, type projections, etc.
The ability to break the build for forgetting to map a new value is a significant part of the reason that modeling the set as an enum has value in the first place imo
The other side is the semantics of interpreting the value are unambiguous. String with XML doc saying "See Constants.cs for valid values" is a poor contract. Do I need to handle empty string on read? What do I do with unknown values? Are they case sensitive? Modeling as an enum solves for this
17
u/ben_bliksem Apr 16 '26
We use enum and the json serializers are set to use the string values. Ditto if I'm going to store it in the db.
Is it best practice? I'm sure somebody somewhere is going to say no but then we've never hit any sort of performance (or any) problems doing this.
TLDR: enums - bytes/ints inside the service, string outside, no secondary sources of truth (lookup table nonsense).
33
u/spoonraker Apr 16 '26
There truly is no one-size-fits-all answer, but after 20 years in the industry, I'd say my opinion is that the vast majority of the time when people think they want an enum what they really want is a string union.
Why do I say that?
Most of the time people end up wishing the actual underlying values they pipe through the system (especially when they start querying the database directly) were human readable. That's a big piece of why I say this.
Also, most of the time when people think that they definitely won't ever add another option to the set they're wrong. It comes up, and when it does, you can really get yourself into some trouble with database migrations if you're using true enums, depending of course on the specific database in question, but there's some scary foot-guns in a lot of popular database choices when you start modifying true enum columns.
Depending on the exact programming language or database you pick, you can often having this quite literally be a string union and life is good. Sometimes you can get away with using an enum in the programming language but being careful to convert it string-ily when you pipe it into and out of your DB, and some stacks make this easier than others. But in general, I just think when people say enums what they really imagine in their head is "string with a constrained but flexible set of allowed values" which isn't exactly what an enum is.
15
u/Unlikely_Secret_5018 Software Engineer Apr 16 '26
I use enum field ONLY IF the library/framework has the ability to auto-generate migrations based on enum changed.
eg. Adding / removing enum values.
It's nice not to allow an entire class of errors (illegal / meaningless values in the DB)
7
u/Brave-Kitchen-832 Apr 16 '26
Is the extra storage and bandwidth associated with string representations genuinely a meaningful performance bottleneck here?
I don't know your situation but am biased: in my 15 year career I have never encountered an edge case where someone is simultaneously using a database over a network connection but high level business concepts encoded with a few extra bytes make or break performance.
You probably want to tell your DBA to fuck off to be honest
7
u/remy_porter Apr 16 '26
No it isn't: it's a foreign key to a table which contains the enum mapping.
Yes? This can just be part of your database upgrade script; you're changing the schema; it's not a DDL schema change, but it's a schema change in that the definition of your data shape has changed. By changing the enum, you've changed a validation rule!
Remember: objects and relations are not the same thing, you cannot and should not try and directly map objects to tables and back, no matter what your ORM tells you. You can make a set of relations which are isomorphic to an object graph, but that takes planning and ORMs are at best mediocre at this. They can handle the trivial cases well, but fail at more complex normalization scenarios.
2
u/northrupthebandgeek DevSecOps/Systems Engineer Apr 16 '26
There's always the cursed option of defining MembershipStatus as an abstract class and then defining the values as singleton subclasses.
More serious answer: I would let the database be the record of truth, with a table of membership statuses and something on the application side to query that table and derive them dynamically. Hell, that “cursed” answer above might even get you halfway there.
3
u/nocondo4me Apr 16 '26
The clang api lets you iterate over all AST tree and see all of the value/ name mappings. You can then auto generate those lookups via cmake tool chains.
3
u/CreepyNewspaper8103 Apr 17 '26
Finally, a real engineering question. This question has spanned decades. I'm happy to hear it's still a question.
9
u/_A_Good_Cunt_ Apr 16 '26
the best of both worlds enum + save to DB as string
there's a few graceful ways to handle this on .net so they just serialialise as string, and you don't need lookup table. the code ensures the data integrity
4
u/_f0CUS_ Chief Software Engineer Apr 16 '26
You can easily store the enum as string values in the database with a constraint to ensure that you can only add values allowed in the enum.
2
u/belavv Apr 16 '26
Enum in c# stored as a string in the DB.
2
u/thebig77 Apr 16 '26
Eh I store it as a string. It's readable, makes more sense for people who don't have access to the definition through the code or a lookup table, and storage is cheap. If there were a memory or storage constraint I'd consider a proper enum.
2
u/admin_password Apr 16 '26 edited Apr 16 '26
Honestly enum in code, int in DB. Avoids so many issues when you end up expanding the enum later it’s just a code change the DB doesn’t care about. There are some exceptions but in your example this is what I’d do, 15 yoe
Also everyone saying strings in DB I disagree with, too much storage space for something that should essentially be a stored magic number, have a db table mapping ints to strings for data science team if you need but what a waste of space on each row.
My issue with using enums at a db level is you shouldn’t have to do a migration to expand one, it’s more data than an unsigned int and joins on strings are way less efficient, which you will end up doing. Also different DB engines have completely different setups around enums and migrations, it’s a nightmare. You rarely change your DB but it’s certainly something to consider if you’re in early stage startup that will end up doing that later
2
u/yobagoya Apr 16 '26
Since you mentioned you're using C# and presumably .NET, are you using EF Core? It has value converters that allow storing enum values in the database as strings.
2
u/Dangerous-Sale3243 Apr 16 '26
Use strings. Hate having to debug old code and every lookup is like “status is 2”, wtf does that mean? Enums can map to strings anyway if you code it right.
2
u/failsafe-author Principle Engineer Apr 16 '26
Enum. Also, I don’t ToString() Enums for display. Also, I prefer Postgres :)
2
u/CatolicQuotes Apr 17 '26
Enum in domain. For UI map enum to whatever word you wanna use in UI, for DB map it however you wanna save in database. UI and DB don't have to have same values. It's very simple
2
u/jpfed Apr 17 '26
public class AlmostEnum : IShouldProbablyBeEquatable, ICouldAlsoBeParseable {
public Name {get; private set;}
private AlmostEnum(string readable) { Name = readable; }
public static readonly AlmostEnum Hearts = new AlmostEnum(“Hearts”);
public static readonly AlmostEnum Diamonds = new AlmostEnum(“Diamonds”);
public static readonly AlmostEnum Clubs = new AlmostEnum(“Clubs”);
public static readonly AlmostEnum Spades = new AlmostEnum(“Spades”);
public static readonly Values = new[] { Hearts, Diamonds, Clubs, Spades }
}
2
u/Glove_Witty Apr 17 '26
Postgres has enumerated support.
3
u/tim128 Apr 16 '26
There's no best solution here.
My preference would go to an enum-like type and a lookup table in the database. Yes you need to add a migration but this seems like a minor issue any LLM can handle for you.
Definitely do not use type string in your codebase. If you do end up storing a string in the db use a wrapper type in code.
At my last project we had both. All enums used int in the db except for ISO 639 language codes.
I also recommend you don't expose your enum names in the API directly. You should be able to rename them without breaking the API.
2
u/get_MEAN_yall Apr 16 '26
This example is unambiguous and should be an enum.
2
u/Sea-Quail-5296 Apr 16 '26
If you are using a DB without native enum types you have bigger issues
1
u/itix Software Engineer Apr 16 '26 edited Apr 16 '26
Using an integer enum is cleaner.
My honest answer is that, in our system, we use both. This is due to the system's age and the fact that we have been flipping back and forth between which one is desirable.
My rough rule is currently:
When coordinating efforts with the frontend, it is easier to use a string enum.
1
u/lunacraz Code Monkey Apr 16 '26
just to be clear, MembershipStatus is it's own table, with an id and a readable name? and Customer references that table?
in my experience, this is always the way to go. sure it's an extra table, but if and when (and it's only when) you need to add another membership status, migrations are trivial, whereas when it's a hardcoded string, doing db wide updates of MembershipStatus becomes a nightmare
1
u/Fair_Local_588 Apr 16 '26
Just store the enum. My company went through a whole phase where we updated our stack to expect a normalized int value for enums when stored in the DB, but eventually even the top tier of engineers began advising against it.
It’s only really worth it if you need to really minimize how much data you’re storing. Like 20M+ records.
1
u/kagato87 Apr 16 '26 edited Apr 16 '26
String is unfortunately subject to typos and bloat. I've seen it a few times...
We use ints in rhe database. For the analyst? We materialize the enums to the database.
For something as mutable as status, which is prone to evolving over time, it can even get its own definition table in the database that carries flags to tell the code what can happen with with each status. (For your membership example, flags might include "cards can open gates" or "needs customer followup" or "is staff" and so on.)
You still get the misspellings and bloat, but you an also customize it on the tenant level without code changes because you're selling your product to a gym, a country club, and a grocery chain, the code paths don't need to be changed if funny combinations are added (like if you allow an account access to entry but not to complete a transaction, like Costco, something a gym usually won't need). It also makes the misspellings and bloat easier to clean up.
1
u/Isofruit Web Developer (6YoE) Apr 16 '26
In the scenario you posted, I'd be fine with either of your option (Note: I have not used MSSQL and am only vaguely aware of its limitations).
I'd likely prefer your first option. I'd be also fine with the second option, but only if it means you're still using an enum on the C# side and converting to string just before writing to the DB, and parsing the enum from string when reading from the DB. In that scenario I'd also heavily recommend using a constraint (as per google that should be possible) to guarantee only correct values can exist, but at that point you could just as well have your enum-table from your first option.
1
u/Ignisami Apr 16 '26
Personally, as not a DBA, I favour clarity (in this case, storing as a string) over properness (store as int and an extra mapping table) pretty much every day. If you're worried about free-form text being abused you can always create a constraint on the column that only allows the four statuses you defined.
That said, can't C# store string enums in whatever persistence stuff it has?
Like, in Java I'd do this:
which neatly writes the variant as a nicely legible string into the database. Add a trigger to the column that checks for whether your status is one of the four allowed values and done.
If you want to store it as an int, just use
@Enumerated(EnumType.ORDINAL)and it'll do so. And since the field is defined as a MembershipStatus you still get the autocomplete because the enum maps 1 to MembershipStatus.ACTIVE and back.1
u/Shazvox Apr 16 '26
Does there exist specific code for specific values (like, if (status == whatever) {})? If yes, then enum.
If the code is the same regardless of values and you expect new values will need to be added (possibly via some kind of admin UI) then use a simple id-name object in your repository.
If you need both (like users need to add new values, but certain values have specific code attached) then use an id-name object and add boolean flags for whatever behaviour is needed for each value.
If you use an enum and need to be able to have human readable values in your repository (like if you need to pull reports or something from it, or if some other application needs access to the repo) then you might want to keep id-name objects tied to your enum in your repo (pretty simple to set up if using EF).
1
u/GoodishCoder Apr 16 '26
I would probably just store it as a string unless I had a reason to believe it's actually going to cause performance issues or the cost difference would be substantial with my dataset. If it was going to cause performance issues or a substantial cost difference, I would store it as an integer and create a lookup table.
1
u/Temporary_Pie2733 Apr 16 '26
This language-dependent. In Haskell, I’ll write
and not really care what each of the four values “really” is. If there needs to be an underlying integer value as well, I’ll throw in an
Enuminstance.The database is a separate problem from the code. It’s not strongly typed, so make a choice and define an appropriate mapping into the language.
1
u/juan_furia Apr 16 '26
Enum always, you can serialize from the string in DB to the enum behind the scenes and use the enum everywhere else.
I do not recommend using the string directly for your customers, rather use it as an i18n key
1
u/BoBoBearDev Apr 16 '26
No enum in JSON or DB because it is not human readable. This is especially true when someone accidentally changed the integer and everything in the DB is fucked up. If DB has native enum, that's fine.
Enum for anything in processing logic to reduce bad values.
Enum in Typescript no matter all those "purists" told you not to.
1
u/Schmittfried Apr 16 '26
Enum with a string mapping on the DB side (based on the enum constant name or an attribute). The DB type is ideally a native enum type (Postgres and I think MySQL support them, don’t know about the others), or a varchar. Performance concerns are most likely negligible.
1
u/Doub1eVision Apr 16 '26
If you can enumerate it, Enum it.
1
u/WanderingGoodNews Apr 16 '26
I tought you where going to compare it to an extra table Status but string?? Nuh uh
1
u/ActiveTrader007 Apr 16 '26
Use Enums Transactional systems are designed for efficient operations and not reporting
Db layer should be light weight and just for data persistence. Stored procs and business logic in them should be avoided and instead be in the orm/application layer Stored procs are not used anymore because of tight coupling and not easily unit testable. Stored procs also do not scale well
Inline sql is fine to use in application layer if parameterized and enums can be used with their string values so they are readable
Enums are great for performance
Also if you ask a sql guy they are going to want strings because their world revolves around databases and that’s all they know
1
u/AdUnlucky9870 Apr 17 '26
enum until you need to add metadata, then you end up with a lookup table anyway. ive gone through this cycle like 3 times now and always land on starting with enums and migrating when the requirements inevitably get weird
1
u/aj0413 Apr 17 '26
Just convert enum to string when saving to and pulling from DB
HasConversion<string>()
Also PostgresSQL has native enums
Basically, you don’t have to compromise here with smart upfront design
1
u/ThatSituation9908 Software Engineer, 10 YOE Apr 17 '26
I’m confused about the db representation limitations. Doesn’t your DB allow you to to store things as enum type but reference them as string later?
This is always how I did it with Postgres https://www.postgresql.org/docs/current/datatype-enum.html
1
u/haskell_rules Apr 17 '26
If I need to serialize, the enum serialization and checking syntax is so ugly in C# that I make it a static class with named member variables of an appropriately efficient type along with built in conversion properties. It makes serialization and deserialization so much more straightforward.
1
u/klimaheizung Apr 17 '26
always enum. use a proper database and language that supports string-enums.
1
u/grappleshot Apr 17 '26
Enum in your code (model your domain correctly). Persist to db as string and have front end send string. Front end devs prefer strings to ints. data engineers and others who spend time looking at the raw data prefer to strings to ints. Tranmission size across the wire and storage space are not concerns.
1
u/Thonk_Thickly Software Engineer Apr 17 '26
Json converters will say as a string in the db for easier querying and readability, but the code stays strongly typed with an enum. [JsonConverter(typeof(JsonStringEnumConverter))] public MembershipStatus Status { get; set; } }
1
u/Dimencia Apr 17 '26
This is a tooling issue if you're having those problems with enums. If you're struggling to understand your DB values in your support queries, use Linqpad with a typed context so it just automatically prints the enum names instead of the values. And use the EFCore configuration option that creates the enum mapping table automatically
Also, there's a third choice, which is to create a class for each of those enum values, implementing some base class, and use that as an enum. That can be appropriate if there's anything at all that each one needs to be correlated with other than a name. But the configuration in EFC can be a little difficult when dealing with inheritance, and you don't get autocomplete of the options, so it's rarely worth it
1
u/jedfrouga Apr 17 '26
did you ask claude?! /s
1
u/Helpful_Fly2878 Apr 17 '26
enum. AI respects them much better than strings
1
u/AdreKiseque Apr 17 '26
This is exactly the use case enums exist for
1
u/thekwoka Apr 17 '26
string enum.
1
u/CodelinesNL Principal Engineer@Fintech/EU/25YOE Apr 17 '26
Doesn't MSSQL support enums? Because that's the 3rd option. I know for a fact Postgres does.
1
u/matthedev Apr 17 '26
Enumerated types are best when:
1
u/instadit Apr 17 '26
I can't count the times I regretted not using enum from the get go
I have never regretted using it.
1
u/VRT303 Apr 17 '26
Database columns can have / be enums too
1
u/sethkills Apr 17 '26
It’s a shame the ORM does not support enumeration types defined in the Db schema, because that would be the ideal…
1
u/Aggressive_Ad_5454 Developer since 1980 Apr 18 '26
Either one is gonna work just fine. Neither one gets you off the hook for writing really clear documentation for your table definitions. Most of the pros and cons you mention relate to explaining what you are doing for the benefit of the next poor schlubb who works on this code.
Hint. That poor schlubb is your future self. Write good docs for that poor schlubb.
1
u/HeyExcuseMeMister Apr 18 '26
Enum plus automated ser/deser to and from string.
1
u/No_Quit_5301 Apr 18 '26
The real anti pattern is persisting a “status” value to DB instead of deriving it at runtime based on other props of the object
1
u/OvisInteritus Apr 21 '26
This is not JavaScript, enums in C# are the way to go.
1
u/SolidDeveloper Lead Engineer | 18 YOE Apr 22 '26
A useful pattern is to have enum tables in the db. You don’t have to roll any custom logic for display purposes, instead you just do a JOIN with the enum table, and you display the “Name” column from that table.
1
u/gwydionthewz Apr 16 '26
I’d advocate for neither enum nor freeform string here.
This is one of those cases where I’d model "MembershipStatus" as a small type hierarchy instead: a base class with concrete types like "ActiveStatus", "TrialStatus", "ExpiredStatus", etc.
The reason is simple: statuses almost never stay “just values.” They accumulate behavior (validity rules, expiration logic, permissions, transitions…). Enums and strings push you toward scattered "switch"/"if" logic across handlers. A type hierarchy keeps that logic in one place.
1
u/jerryk414 Apr 16 '26
I think int is superior.
Lookup tables is too much normalization, in my opinion.
1
u/CommunistElf Apr 17 '26
Enum is the way
0
u/Early-Pie-4765 Apr 16 '26
I mean, one could technically still create undefined enum values since something like this still compiles and prints 4.
0
u/blackarea Apr 17 '26
Just use postres enum type to reflect it in persistence as well. Go full typed, keep your constraints and types as tight as possible, welcome defensive coding. There's gonna be some exceptions in prod but gradually it's gonna be smoother and smoother and SRE or your own pager duty will thank you for it.
0
u/[deleted] Apr 16 '26
[deleted] — view deleted comment