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.

learn more… | top users | synonyms

35
votes
8answers
5k views

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 ...
32
votes
4answers
25k views

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. ...
30
votes
5answers
6k views

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 ...
17
votes
5answers
4k views

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 ...
16
votes
3answers
4k views

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 ...
13
votes
3answers
4k views

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 ...
13
votes
2answers
959 views

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 ...
11
votes
5answers
3k views

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 ...
10
votes
7answers
6k views

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 ...
9
votes
4answers
3k views

Keeping an enum and a table in sync

I'm making a program that will post data to a database, and I've run into a pattern that I'm sure is familiar: A short table of most-likely (very strongly likely) fixed values that serve as an enum. ...
9
votes
1answer
341 views

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 ...
8
votes
2answers
488 views

Why is Flags Enumeration considered an intermediate skill?

I was reading this article: Designing Flags Enumerations @ msdn and it says Combining flags enumeration values is an intermediate skill that should not be required for developers implementing ...
7
votes
7answers
2k views

Why isn't there a next operation on enums?

In most popular programming languages like Java and C# there is a way to define enums, which are essentially datatypes with a fixed set of values, e.g. DayOfWeek. The problem is, given a value, e.g. ...
7
votes
3answers
1k views

What are the common misuses of “enum” in C?

I have seen C code where people used enum heavily. But all it does is confuse others. In many places plain integers can do the same thing with less ambiguity. What are the common misuses of enum?
6
votes
3answers
9k views

How to use a switch statement with enum efficiently?

I would like to know how I can use a switch statement with enum values for the following scenarios: I am making a small program for a flight reservation system. The program is meant to enter certain ...
6
votes
2answers
4k views

Nested Enum type in C++ or C#?

I've come across a recurring issue in a few of my recent projects in which I find myself using enums to represent state, or type, or something else, and I need to check against a few conditions. Some ...
6
votes
2answers
1k views

Java: would you use an EnumSet in this case?

I have this weird scenario in which some Java property is used to build and HQL query, and the type of the field is Boolean, i.e. it is boxed. I wondered why because I don't really like to think of ...
5
votes
5answers
3k views

In what situations does it make sense to use an enumeration when writing object-oriented code?

Enumerations1 are often associated with procedural code rather than object-oriented code. They tend to give rise to similar switch statements scattered through the code, and in general, these are ...
5
votes
3answers
447 views

Is enum order sensitivity an antipattern?

Is it an anti-pattern to depend on a particular order of an enum's instance declarations? For example, consider: public enum CompassPoint { North, East, South, West; } These points ...
5
votes
5answers
3k views

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 ...
5
votes
1answer
389 views

is switch(this) antipattern or bad practice in Java for Enums?

At work I bumped into a problem to see how enums are growing and storing business logics. As they grew, the constructors grew a lot. At one point I found out instead of putting in let's say the tenth ...
5
votes
2answers
990 views

Is it better to use strings or int to reference enums outside the java part of the system?

We were having a discussion at my work about the use of enums in Java. A coworker was arguing that when using enums on the server-side, whenever required we should use string to reference to it (for ...
5
votes
4answers
4k views

Should I create an Enum mapping to my database table

I have a database table containing a list of systems relevant to the tool I am building, mostly in-house applications, or third-party systems we receive data from. This table is added to infrequently, ...
4
votes
2answers
709 views

How can I avoid these nested repetitive ifs?

I'm trying to implement a web interface for a user database. Hosts can create guests for their courses, the guests can get deleted after the course has ended but have to remain in the database for a ...
4
votes
2answers
3k views

Best approach for multilingual Java Enum

I'm running into issues with an approach I am taking and am now wondering if I just started down the wrong path and should rethink my approach. Here is what I attempting. I would like to use an Enum ...
4
votes
2answers
9k views

Best practice for packing Java enums? [closed]

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 ?
4
votes
2answers
4k views

Why would one ever want to use a synchronized method in Enum?

I stumbled upon this question and a couple of other along the same lines. While we know creation of enum is thread safe and enums are by birth singletons . It kind of confuses me that why would ...
4
votes
3answers
218 views

Enums and single responsibility principle (SRP)

I have been experimenting lately with enums, and I found out that in Java they can do much more than simply representing a fixed set of constants. Now, I am thinking about creating a new enum for my ...
4
votes
6answers
2k views

Explicitly define enum values, even if the default value is the same?

There are times when an enum's values are important: it is not necessary for them to be unique, they also need to have specific values. In such cases, should the values be explicitly defined, even if ...
4
votes
2answers
877 views

Enums in java switch-statements and completeness

I feel like I should be able to find an answer to this, but it turns out to be harder to search than expected... so: In C#, when we do something like: enum MyEnumClass { A, B }; static String ...
4
votes
2answers
847 views

Is it a good practice to burn business logic into Enums?

Let's have a simplified business logic like this: public enum BusinessLogic { STAGE_ONE(true, false, false), STAGE_TWO(true, true, false), STAGE_THREE(false, false, true); private final ...
4
votes
2answers
456 views

Do I really want a dynamic Enum, or something else? Better caching? A Struct?

This question is asked on SO and this site often it looks like. "How do I do a dynamic enum?" Answers range from you shouldn't to possible compile time solutions. Now my question is do I want a ...
4
votes
1answer
93 views

Can't I use an enum at application level if I have a NxN relationship?

After reading the answers to this question, and this and this related questions, I'm now confused about how/if to work with an enum having an NxN relationship. Let's say I have an entity Hotel, an my ...
4
votes
2answers
3k views

Is it possible to avoid enormously big switch in that case? [duplicate]

I'm writing a simple chess-related code with intention to write it clearly (performance doesn't matter at all). And this method I have doesn't look clean to me at all: public static Piece ...
3
votes
5answers
1k views

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 ...
3
votes
3answers
1k views

How to deal with almost the same enums?

I need to define enums in several classes. The majority of fields are the same in all of the enums. But one has one or two more fields, another has fewer fields. Now I wonder what is the best way to ...
3
votes
1answer
1k views

Could someone explain HaXe enums?

I have AS3/PHP background and some basic understaning of enums from C++. Saying shortly: I read this: http://haxe.org/ref/enums http://haxe.org/doc/cross/more_on_enum And though - good ...
3
votes
1answer
574 views

Which is better to use? (Enum or Constant)

I have my enum like this. enum Size{PAGE_SIZE=20, HEADER_SIZE=30 }; I only use them as constants(No enum variable created) in my program. i.e= int x = PAGE_SIZE + 20; So what is the ...
3
votes
3answers
3k views

Compatibility of Enum Vs. string constants

I was recently told that using Enum: public enum TaskEndState { Error, Completed, Running } may have compatibility/serialization issues, and thus sometimes it's better to use const string: public ...
2
votes
2answers
209 views

What was the first programming language with Enumerations?

I'm reading about Swift enum's in the Swift Programming Language guide and the text was comparing the differences between Swift's enum and C's enum. This made me curious as to where enumerations came ...
2
votes
1answer
242 views

Why use a enum to create the singleton pattern [duplicate]

Why would you use an enum to create a singleton pattern? To what purpose would it serve over a conventional singleton pattern? I have seen the above used. The code uses an enum to create this pattern ...
2
votes
3answers
4k views

In what situation do Entity Framework enums become useful?

I am working on a project where there will be plenty of static options being stored in the database. I looked at using Enums for this, but do not see how they could be useful. They do not create any ...
2
votes
2answers
267 views

Allowing enum to express a valid combination of values

Suppose in a manufacturing environment, there are certain stock materials available for use in a product. For example, there are only a few different sizes of copper tube, each having a specific ...
2
votes
2answers
847 views

Is switch-case over enumeration bad practice?

I have an enumeration with the commands Play, Stop and Pause for a media player. In two classes I do a switch-case over the received commands. The player runs in a different thread and I deliver the ...
2
votes
1answer
107 views

Use of project-specific values with enum defined in class library

We have an enum in a class library: Public Enum FieldType Phone Span Gender DrawPath .... End Enum which we use with an attribute applied to properties, for multiple scenarios: ...
2
votes
1answer
647 views

Several enum classes that declare constants with the same names

I'm working on a Java library for sorts for Magic: the Gathering. Well, without going into a lot of detail, in the game there are five different colors of magic: white, blue, black, red, and green. ...
2
votes
2answers
644 views

Are the IETF BCP 47 language tags defined as enums anywhere in JDK?

Are the IETF BCP 47 language tags defined as enums anywhere in JDK? For Locale.forLanguageTag() we pass values like fr-FR, jp-JP etc. Are there any enums already provided by JDK for it? Or should ...
2
votes
1answer
804 views

constants/enums in API

What are a few ways constants and enums are dealt with when creating an API Client? I'm writing a client in python for our API and I've hit a sticking point with this. We use a lot of mappings to ...
2
votes
1answer
314 views

Static properties and implicit “self” property in structures and enumerations vs classes in Swift

I am currently reading the Swift language documentation and came across these sentences in the chapter about methods: Similarly, type methods on structures and enumerations can access static ...
1
vote
5answers
2k views

How to unit test a missing case in a switch statement where all cases are true

I often use enum types in my code with a switch to apply logic to each type. In these cases it's important that each enum has code implemented. For example; public enum eERROR { REQUIRED, ...