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.
134 questions
5
votes
1
answer
334
views
What do you call an enum that translates its own values?
I see this pattern a lot, especially with countries, or more generally regions. An enum is defined with additional fields and with methods that translate to and from these values.
Example:
import ...
3
votes
3
answers
310
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:
...
2
votes
1
answer
134
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 ...
2
votes
1
answer
672
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 ...
0
votes
3
answers
8k
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),
...
5
votes
6
answers
4k
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 ...
2
votes
1
answer
865
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 ...
3
votes
2
answers
409
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 ...
0
votes
3
answers
3k
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 ...
2
votes
1
answer
4k
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 ...
2
votes
3
answers
1k
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 tables,...
-4
votes
1
answer
170
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 ...
1
vote
2
answers
1k
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 ...
6
votes
3
answers
7k
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 ...
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 ...
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),
...
1
vote
4
answers
906
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
{
}
...
2
votes
3
answers
712
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 {
...
3
votes
0
answers
726
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 ...
4
votes
2
answers
7k
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 ...
0
votes
1
answer
665
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 ...
2
votes
1
answer
2k
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 ...
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 ...
1
vote
3
answers
1k
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, ...
2
votes
3
answers
405
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 ...
1
vote
2
answers
86
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 ...
2
votes
2
answers
1k
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 ...
4
votes
3
answers
778
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 ...
0
votes
5
answers
665
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 ...
1
vote
1
answer
350
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-...
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 ...
1
vote
4
answers
1k
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|) ...
1
vote
0
answers
155
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? ...
1
vote
3
answers
1k
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 ...
1
vote
1
answer
385
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
~...
1
vote
1
answer
340
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 |...
2
votes
2
answers
220
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 ...
7
votes
4
answers
4k
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 ...
-1
votes
1
answer
250
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 ,...
1
vote
1
answer
360
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 ...
0
votes
1
answer
1k
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. ...
4
votes
1
answer
10k
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 ...
1
vote
1
answer
187
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.
...
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 ...
1
vote
1
answer
1k
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("...
4
votes
3
answers
190
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, ...
21
votes
9
answers
86k
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 ...
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 ...
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'
...
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, ...