Enum (also enumeration) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language, and are often predefined with an implied ordering.
Questions tagged [enum]
131 questions
94
votes
8 answers
Why would you store an enum in DB?
I've seen a number of questions, like this, asking for advice on how to store enums in DB. But I wonder why would you do that. So let's say that I have an entity Person with a gender field, and a Gender enum. Then, my person table has a column…

user3748908
- 1,607
- 2
- 14
- 15
74
votes
7 answers
Using scoped enums for bit flags in C++
An enum X : int (C#) or enum class X : int (C++11) is a type that has a hidden inner field of int that can hold any value. In addition, a number of predefined constants of X are defined on the enum. It is possible to cast the enum to its integer…

Daniel A.A. Pelsmaeker
- 2,715
- 3
- 22
- 27
66
votes
4 answers
Is it wasteful to create a new database table instead of using enum data type?
Suppose I have 4 types of services I offer (they are unlikely to change often):
Testing
Design
Programming
Other
Suppose I have 60-80 of actual services that each fall into one of the above categories. For example, 'a service' can be "Test…

Dennis
- 8,157
- 5
- 36
- 68
37
votes
7 answers
When are enums NOT a code smell?
Dilemma
I've been reading a lot of best practice books about object oriented practices, and almost every book I've read had a part where they say that enums are a code smell. I think they've missed the part where they explain when enums are valid.…

stromms
- 505
- 1
- 4
- 6
37
votes
3 answers
How to represent (enum) types in a public API
I am working on a simple API that I want to use for my own client, and to open to the public in the future.
I have "Item" objects which can have different "types". The type is a C "typedef enum", for the moment I have :
typedef enum {
…

Julien
- 473
- 1
- 4
- 5
33
votes
5 answers
Why do we need enums in dynamically typed languages?
I was reading some code here and saw that an enum is used to store names of html tags. Why do we ever need to do this? What benefit do I get using this strategy?
I know that how useful enums are in compiled or statically typed languages but when I…

CodeYogi
- 2,156
- 1
- 18
- 34
21
votes
9 answers
Should one test the values of an enum using unit tests?
If you have an enum with values only (no methods as one could do in Java), and this enum is part of the business definition of the system, should one write unit tests for it?
I was thinking that they should be written, even if they could seem simple…

IS1_SO
- 347
- 1
- 2
- 6
21
votes
6 answers
Is it okay to go against all-caps naming for enums to make their String representation simpler?
Several times I've seen people use title-case or even all lower-case naming for enum constants, for example:
enum Color {
red,
yellow,
green;
}
This makes working with their string form simple and easy, if you want to do throw new…

codebreaker
- 1,694
- 1
- 18
- 24
20
votes
5 answers
switch statement - handling default case when it can't be reached
If I'm using a switch statement to handle values from an enum (which is owned by my class) and I have a case for each possible value - is it worth adding code to handle the "default" case?
enum MyEnum
{
MyFoo,
MyBar,
MyBat
}
MyEnum…

s d
- 322
- 1
- 2
- 9
18
votes
5 answers
Do enums create brittle interfaces?
Consider the example below. Any change to the ColorChoice enum affects all IWindowColor subclasses.
Do enums tend to cause brittle interfaces? Is there something better than an enum to allow for more polymorphic flexibility?
enum class…

Matthew James Briggs
- 1,757
- 3
- 15
- 23
15
votes
4 answers
Is it wrong to use flags for "grouping" enums?
My understanding is that [Flag] enums are typically used for things that can be combined, where the individual values aren't mutually exclusive.
For example:
[Flags]
public enum SomeAttributes
{
Foo = 1 << 0,
Bar = 1 << 1,
Baz = 1 <<…

Mathieu Guindon
- 1,720
- 16
- 33
15
votes
8 answers
Is it a bad practice to include all the enums in one file and use it in multiple classes?
I'm an aspiring game developer, I work on occasional indie games, and for a while I've been doing something which seemed like a bad practice at first, but I really want to get an answer from some experienced programmers here.
Let's say I have a file…

Bugster
- 4,013
- 8
- 37
- 44
14
votes
3 answers
What are the downsides of implementing a singleton with Java's enum?
Traditionally, a singleton is usually implemented as
public class Foo1
{
private static final Foo1 INSTANCE = new Foo1();
public static Foo1 getInstance(){ return INSTANCE; }
private Foo1(){}
public void doo(){ ... }
}
With…

irreputable
- 657
- 5
- 7
13
votes
1 answer
Enum with a lot of boolean properties
I'm currently working on a webapp where we often need to condition some server logic based on the page that is going to be returned to the user.
Each page is given a 4-letter page code, and these page codes are currently listed in a class as static…

Joffrey
- 251
- 1
- 2
- 8
13
votes
2 answers
Best practice for packing Java enums?
What is the best practice for packaging Java enums?
is it separate file for each enum?
or
having same file for all the enums?
What are the pros and cons ?

goodspeed
- 167
- 1
- 2
- 7