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
337
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
317
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
135
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
674
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
872
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
412
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
171
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 ...