Skip to main content

Questions tagged [enum]

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.

Filter by
Sorted by
Tagged with
3 votes
3 answers
235 views

Handle hierarchical relationships between large number of enums

I am working on a C# project and I have a somewhat large number of labels (~100) that have some sort of relationships between one another. Here is a minimal dummy example that illustrates this: ...
evolved_antenna's user avatar
2 votes
1 answer
60 views

Node Services and Enum Sharing

I will get to the question in a minute.... We have 2 in house services that either have an API contract between the 2 that involves an enum or the enum value is stored in a shared database. I don't ...
jdtommy's user avatar
  • 129
1 vote
1 answer
402 views

Representing Rust enums in databases

Although I love Rust's enums (and the idea of making illegal states unrepresentable), I'm struggling to represent (ironic, yes) them in databases like PostgreSQL when the variants have data associated ...
yoshke's user avatar
  • 19
0 votes
3 answers
3k views

Getting an enum value by a unique property in Java

I can write an enum with properties besides the name and ordinal. A very generic example would be: public enum ExampleEnum { EXAMPLE0(Example.example0), EXAMPLE1(Example.example1), ...
CPlus's user avatar
  • 1,189
5 votes
5 answers
2k views

Java Exception Error Enumerations Anti-pattern

Over the years I've many times seen folks enumerate error codes for exceptions in Java. I generally have felt this leads to more tedious code and does not provide value. For example, there will be an ...
Kirby's user avatar
  • 271
2 votes
1 answer
363 views

Elegant way to handle two options, when both is also an option

In the simplest case, I have some code where the user may want to do one thing (a), another thing, (b), or both (a+b). The options are reasonably complex and have their own functions, but I would like ...
QuantumChris's user avatar
3 votes
2 answers
352 views

What is the purpose of using strings as identifiers or flags instead of enumerated types?

I have seen in mainly high level languages, but also in lower level languages, the use of option names as strings to specify one of several fixed options, or as flags, or more generally, code using ...
CPlus's user avatar
  • 1,189
0 votes
3 answers
2k views

Optimizing a string to enum converter [closed]

I have built a string to enum converter, which converts a known set of strings that it receives into enum, in order to satisfy a function pointer. Some of you may recognize some elements of this ...
HFOrangefish's user avatar
2 votes
1 answer
1k views

How to add some data to an Enum in Python

I have a class that I use to define different types of plots I am performing class MyPlots(Enum): STANDARDSCALE = "standard" LOGSCALE = "log" there are default values ...
La Cartuccia's user avatar
2 votes
3 answers
932 views

Enforce correspondence between enum and lookup table

I have seen a common design pattern where there will be a "lookup table" in the database like this: Color ColorId ColorName 1 Blue 2 Red 3 Green with a foreign key constraint on other ...
JoelFan's user avatar
  • 7,091
-4 votes
1 answer
134 views

Loops for enumerated types

I'm currently reading Code Complete by Steve McConnell. In section 12.6, Enumerated Types, he says that we can define first and last entries like limits: Define the first and last entries of an ...
CoderDesu's user avatar
  • 1,015
1 vote
2 answers
830 views

Concept/Design question: Alternatives to switch/conditional statements and Enums

I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine. I have two questions focused on ideal structure and design ...
Bob Jones's user avatar
5 votes
3 answers
5k views

SQL: enum vs reference table when values are referenced in the code

This question considers whether SQL DB data should be represented as an enum or by a reference table, when the application code is aware of particular instances of said data. I'll illustrate with an ...
Moeghoeg's user avatar
3 votes
4 answers
2k views

Alternative to using enum

The project I'm working on has a code dependency on a TeamNames enum. The problem with this is that the project needs to be recompiled and redeployed on any addition/deletion in TeamNames. How can I ...
NiceOne's user avatar
  • 33
2 votes
5 answers
1k views

How can I write an enum for date periods where not all periods have a static number of months?

I have an enum that works very well to represents date periods and the number of months in those date periods: public enum StandardDatePeriod { ONE_MONTH(1), SIX_MONTH(6), ONE_YEAR(12), ...
James's user avatar
  • 285
1 vote
4 answers
786 views

Using Enum to represent VehicleType in cab Booking system

I am working on low level design of cab booking type of system and feeling stuck at modelling Vehicle in Booking class.I came up with following design. Class Vehicle { } ...
stkUser's user avatar
  • 81
2 votes
3 answers
486 views

Enums or Multiple Inherited Classes

I am reading about OOD and came across Parking lot design problem.Parking lot has parking floors which has parking spots.The parking spot class looks as follow: public enum ParkingSpotType { ...
rahul sharma's user avatar
2 votes
0 answers
605 views

Enum versus ValidateSet

Currently, in my PowerShell scripts I am using [ValidateSet()] in my functions in order to limit the options for user input. Now I discovered the Enum as an alternative. I have read a couple of blogs ...
Alex_P's user avatar
  • 151
4 votes
2 answers
3k views

Best way to enforce variable has certain values using enum in Python

I have a string variable named status in my code that can take on three values "Starting", "In-progress", and "Complete". A friend of mine said it was good practice to enforce that status has one of ...
user35734's user avatar
  • 159
0 votes
1 answer
582 views

Name of this enum-based design pattern to get the type

I have been using a pattern in a lot of places (mainly C#) that I would like to know the name of. Here is an example of it in C#: public enum ThingType { A, B, C } public interface ...
Romen's user avatar
  • 111
2 votes
1 answer
1k views

Using Enum constructors, Overridable Getters, or both in combination?

Currently an enum in our project takes properties in the constructor. They are practically guaranteed to be entirely different across every different enum. Now I'd like to add an property which is a ...
Joe's user avatar
  • 405
10 votes
2 answers
2k views

What is the name for the integer part of a enum?

I've been writing some code comments, and I've been trying to refer to the index integer of a enum type, but I'm not sure what it's called. I'm tempted to call it the identifier, however there is ...
DubDub's user avatar
  • 595
1 vote
3 answers
882 views

What options are available to replace an `enum` with a multi-type solution?

A Quick Note I know the question title is a little odd but I haven't had much caffeine this morning so I'm having trouble thinking clearly right now. If you have any suggestions for a better title, ...
Hazel へいぜる's user avatar
2 votes
3 answers
313 views

How to model a Struct with a limited set of values that behaves like an enum?

I have a general organization problem with my code. I'm modeling DNA, and I've created a Nucleobase struct to store the "letter" of the DNA. For ease of use, I would prefer the following functionality ...
Adam B's user avatar
  • 1,645
1 vote
2 answers
71 views

Architecture of "tags" for contacts in a marketing system and hard-coded IDs so functions can refer to them

I have these tables: contacts, tags, contact_tags. (contact_tags has columns contact_id and tag_id.) Administrators can manually create new tags via an internal website. Administrators can also ...
Ryan's user avatar
  • 157
2 votes
2 answers
786 views

How to gradually refactor a Map with string keys to use enum keys?

I'm considering refactoring some Java code that passes around objects that implement the interface Map<String, Object>. The strings are all (as far as I know) from some fixed list of string ...
Ryan1729's user avatar
  • 234
3 votes
3 answers
623 views

Enum design: when is an "open" value set acceptable?

I was looking into the Enum design recommended by Microsoft (see here), and I found a statement that made me stop to think for a while: X DO NOT use an enum for open sets (such as the operating ...
curiousguy_08's user avatar
0 votes
5 answers
499 views

What if there is specific logic for one particular value of an enumeration class?

Background: I have seem some argument for using enumeration classes instead of enum in C#, in particular, this section from a book available at MSDN. On the references there is this "Enums are ...
user1620696's user avatar
  • 4,887
1 vote
1 answer
331 views

Best pattern to solve problem where objects only differ in one attribute

As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
J-P's user avatar
  • 37
3 votes
3 answers
1k views

Is Enum Polymorphism bad for DAO Methods?

I have been going back and forth in a discussion about polymorphic enums to call different DAO methods depending on enum entry, and I haven't been able to get a common agreement on this subject. Lets ...
Danilo Silva's user avatar
1 vote
4 answers
752 views

How would I add up enumerator values such that any combination provides a unique number?

Backstory (You can skip) I am writing a pronunciation library for irregular words. Take something like the following: T1E1s // tee one E one es | tee one E ones 1994-1995// 1994 (minus|dash|to|) ...
Anon's user avatar
  • 3,613
1 vote
0 answers
150 views

Convert between enum and discriminated union

Is there a software pattern (or some recommended guidelines) for how to convert between enum values and discriminated unions? Or more specifically: project a discriminated union onto its enum case? ...
Michael's user avatar
  • 289
1 vote
3 answers
875 views

Using extraneous enum members for bounds/validity checking

A coworker of mine insists that this is the right way to write enums in C# public enum ExampleEnum { InvalidItem, Item1, Item2, Item3, MaxItem } We have enums defined like this ...
chanban's user avatar
  • 121
1 vote
1 answer
351 views

How to understand and design functions with enumeration arguments that work in tandem with bitwise operators in C++ and Qt?

In the C++, there are 6 bitwise operators: Symbol Operator & bitwise AND | bitwise inclusive OR ^ bitwise XOR (eXclusive OR) << left shift >> right shift ~...
Anon's user avatar
  • 3,613
1 vote
1 answer
320 views

Multilingual reference/lookup tables and reference tables with enums

I will use the reference tables for the first time so I have some questions about it. Example Table ------------------------ id | description | ------------------------ 1 | Some description1 |...
mnesimi's user avatar
  • 21
2 votes
2 answers
140 views

Using a Collection with Attributes to Check and Make Decisions

I was reading on this SESE page about using a variable to indicate the object type, more specifically, an enum. The accepted answer states: When your weapon types enum just mirrors the class ...
user avatar
7 votes
4 answers
3k views

DB Enumerations vs App Enumerations vs Lookup tables

We started a project recently, and during the design process, we have some arguments among team members. We have to deal with enumerations so we ended up with three choices. Have the enumerations as ...
pik4's user avatar
  • 385
-1 votes
1 answer
234 views

Few unusual C/C++ declarations

I came across this Enum and Struct declarations in a project supposedly done by an expert. The declarations / definitions are little different than what im used to so far. enum EnumKeys { KEY_MENU ,...
ABCD's user avatar
  • 117
1 vote
1 answer
326 views

Keep enumeration in DB when data associated with it is not storable in DB

I've seen several questions on this website explaining that enums should be kept in the database when there is one (this one for example). However, I feel like my case is different and there are extra ...
ZamenWolk's user avatar
0 votes
1 answer
982 views

Enum as part of domain - enum + extension or consolidate into one class?

I have a program where the domain is focused around programs. As part of the domain, I have a 'ProgramType', which is an enum formed mostly via a string from the database but also via a bit of logic. ...
Sarov's user avatar
  • 403
4 votes
1 answer
9k views

How to improve a pattern 'enum class with methods'

Over many years, I always find myself reconsidering this design, so I wanted to get some feedback on my solution to it. Problem: I need a limited amount of objects = instances from a class, and I ...
Aganju's user avatar
  • 1,463
1 vote
1 answer
139 views

What is the solution of enum based dynamic states and actions used in a project

I have recently started working on a project where we have a workflow engine which has flexibility to add dynamic states and corresponding actions for each state and all these are stored in database. ...
digi's user avatar
  • 111
4 votes
1 answer
4k views

Enum or reference table when dealing with maps

I am storing information about Widgets in my database, and each Widget has one (non-unique) transformation function associated with it. My problem is how to associate Widgets with their transformation ...
Plasma's user avatar
  • 151
1 vote
1 answer
798 views

Enum or dictionary for inmemory parameter

I have a table UserItems {ID, UserID, ItemID} Items must be in RAM (not in db table). What is better for this? As enum with attributes or as dictionary of items?: enum Items { [InternalParam("...
Glebka's user avatar
  • 141
4 votes
3 answers
175 views

Changing bot application state (Starting → Started → Stopping → Stopped)

I am developing a bot which I need to start and stop at will. I have 2 buttons in my form, StartButton and StopButton which change the state. The Bot has 4 possible states: starting, started, ...
Joao Vitor's user avatar
21 votes
9 answers
81k views

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 ...
IS1_SO's user avatar
  • 347
11 votes
7 answers
1k views

Is it a good practice to have a special value "ALL" in an enum

I'm developing a new service in a micro-services environment. This is a REST service. For simplicity, let's say that the path is: /historyBooks And the POST method for this path creates a new history ...
Ron Klein's user avatar
  • 691
1 vote
1 answer
2k views

What's the most idiomatic way to make a collection of enum.Enum in Python?

I have an Enum in Python that looks something like this: import enum class Color(enum.Enum): red = 'red' blue = 'blue' yellow = 'yellow' puce = 'puce' chartreuse = 'chartreuse' ...
Wayne Werner's user avatar
  • 2,370
1 vote
3 answers
7k views

Utility class or helper function in class?

I'm Working with php and we using enum's by create an Abstract class that mocking enum behavior - but my question is cross-languages. Where all the helper functions like toString, getEnumTypes, ...
Michael's user avatar
  • 197
4 votes
2 answers
998 views

Is there an Ideal way of storing namespace-related elements?

In the title, with namespace-related elements, I refer to Enums, Delegates and other elements that do not belong to a single class, but to the whole namespace, or application. I know that I can cram ...
oneManArmin's user avatar