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
4
votes
2
answers
1k
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 ...
2
votes
3
answers
640
views
If an enum is used without bitwise operation, and all of its values are predefined individually, is it violating open closed principle?
For example, if I use an enum, which all values are manually defined individually, for example:
public enum MyNum{
Zero(0),
One(1),
Two(2);
private final int value;
MyNum(int ...
15
votes
4
answers
8k
views
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
{
...
1
vote
2
answers
11k
views
C# is it better to use string or an enum as a key?
Example 1 (used at the moment)
If (BodyPart["Right Leg"].BodyStatusEffects["Active"].Active)
{
BodyPart["Right Leg"].BodyStatusImpacts["Poisoned"].Active = false;
BodyPart["Torso"]....
5
votes
2
answers
3k
views
Semantic Versioning in OpenAPI with string "enum": new values OK?
My team is preparing to add new capabilities to an OpenAPI contract and our implementation of it. There are pre-existing clients. We are planning to take our API from v1.1 to v1.2 while fully ...
0
votes
1
answer
7k
views
Is it a good practice to write an enum as a class file in java?
I am constructing an enum Flower which contains the fixed set of constants for Flowers as described below. I'm writing it in a separate file and importing it to other classes so that it could be used ...
0
votes
3
answers
2k
views
Why do the following enum fields extend their base class or base enum?
Regarding enums in java how I understood is
Sample enum
public enum Strategy {
STRATEGY_A {
@Override
void execute(){
System.out.print("Executing strategy A");
...
2
votes
2
answers
934
views
Static per-enum data: constructor, set in initialiser or override getter?
I have an enum with > 10 items each having 8 static properties. Contrived example:
enum JavaTypes {
INTEGER,
BOOLEAN,
STRING,
...;
boolean isPrimitive() {
}
boolean ...
2
votes
1
answer
6k
views
ASP.NET Web API: enum or strings
I have an ASP.NET Web API application. It uses enums for some fixed sets of states or types. Now I need to extend enum to support more values. But it will break backwards compatibility so I need to ...
2
votes
2
answers
300
views
What should you replace an enumeration with if values are to be provided by plugins?
I'm working on a piece of software which generates configuration data for certain hardware and currently needs to be adapted each time the hardware is released in a new version by an external company (...
2
votes
2
answers
16k
views
Mapping enum values into regexes
I'm doing some code cleanup and I'm looking at my regexes. I have an extremely simple one:
(ARA|CHI|FRE|GER|ITA|JPN|RUS|SPA)\s[0-9]{3}-[0-9]{2}
It basically validates course identifiers for a ...
0
votes
0
answers
277
views
Can I keep my code loosely coupled with public-facing enums?
I have some enums in a concrete API/library that will be publicly used by application projects. My problem here, is that I cannot write an interface to these enums (that I know of). I should have ...
1
vote
1
answer
92
views
Is an all-encompassing enumerator appropriate?
I will illustrate the problem with a specific case. Suppose we have a bit-flag style enumeration type defining different kinds of validations. It's tempting to define an enumerator like ...
3
votes
3
answers
774
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 ...
33
votes
5
answers
8k
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 ...
1
vote
2
answers
5k
views
What is the best data structure to store 2 keys and 3 values?
Well, I am having two keys and 3 values for that. Say key1,key2,value1,value2,value3. In future may the values can be increased like value4, value5 so on.
I need to get the values(value1,value2,...
1
vote
0
answers
122
views
How to structure enum data to achieve the desired effect
I'm making a framework for building simple html websites for an embedded system and I want to make it bulletproof in a way that a user can't make mistakes in building the html document. As I've ...
1
vote
2
answers
280
views
Would adding enums to my class make my code more compact or efficient?
I am working on a C# class library for my colleagues to use when programming industrial cameras.
I'm trying to determine both the most compact and elegant method to write the class. The commands are ...
1
vote
4
answers
308
views
Will returning an enum, instead of String, be too restrictive here:
Currently, there are only 3 possible publishers. I might want to add some more in the future:
interface NewsArticle {
enum Publisher { NYPost, ChiTribune, LATimes }
Publisher getPublisher();
...
2
votes
2
answers
7k
views
Whether to use enum vs map vs config file?
I have ~30 resources each having ~10 attributes. I want to store some information about each attribute. Ex: its multiplicity, it RW (Read/Write), RO (Read only), longName, shortname.
So I was ...
14
votes
1
answer
12k
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 ...
4
votes
1
answer
173
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 ...
99
votes
8
answers
114k
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 ...
7
votes
2
answers
5k
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 ...
2
votes
1
answer
946
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
1
answer
1k
views
Understanding Enums in Java
In the book Effective Java its told that:
The basic idea behind Java’s enum types is simple: they are classes that export
one instance for each enumeration constant via a public static final field....
42
votes
7
answers
52k
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
2
answers
998
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 ...
70
votes
4
answers
40k
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 categories....
3
votes
3
answers
3k
views
Would combining enums with static strings in java be sloppy?
Currently my team has a number of constants defined as static final strings. I want to be able to iterate over these strings as if they were an enum in one location, but everywhere else they are used ...
11
votes
3
answers
12k
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 ...
1
vote
2
answers
3k
views
One Enum vs Multiple Enums
I am developing an application where a user submits a mission and other users accept the mission.
Pretty simple.
I want to keep a track of the mission progress status and store it into a database.
...
25
votes
6
answers
49k
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 simple ...
4
votes
1
answer
5k
views
Java partial enum backed by the database
I have following problem:
need to use enum in my java code, since I'll have to ask in my business logic things like if(someting == enumname.VALUE_ENUM){... but I don't have all the enum types at the ...
3
votes
1
answer
752
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 ...
19
votes
5
answers
13k
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 ...
4
votes
3
answers
14k
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 Test(...
3
votes
2
answers
1k
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 ...
1
vote
0
answers
307
views
Should I use enums or arrays for a dynamic data source?
I've inherited an iOS project that uses NS_Enum to store the number of rows and sections in a UITableView At the moment, the UITableView is static; no rows are being added to it. However, that is what ...
1
vote
0
answers
804
views
Enforce coding decision to include or exclude an Enum element in an EnumSet at compile time
I would like to enforce that the elements of a Java Enum are chosen or excluded from an EnumSet at compile time
i.e. I am forced to make the decision to put it in the set or not whenever I create a ...
1
vote
1
answer
791
views
Open closed principle vs abstraction leaking (Java enums)
In Java, an enum is not a plain replacement for a number (like in C/C++), but a family of objects which can have properties. For instance
public enum Order {
NAME("Ordering by name"),
SURNAME("...
5
votes
2
answers
6k
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 ...
0
votes
1
answer
623
views
Enum as singleton or fully functional class [duplicate]
Joshua Bloch claims that
"a single-element enumeration type is the best way to implement a
singleton"
Why? I totally disagree with this statement because enumeration is data type with some type-...
10
votes
3
answers
15k
views
Nullable enumeration values vs. "NoValue" or "Undefined", etc
I often write code which translates entities in the database to domain objects. These entities often have fields which are constrained and translate to enumerations in the domain objects. In some ...
2
votes
1
answer
652
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 ...
11
votes
2
answers
10k
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 ...
1
vote
2
answers
2k
views
Enumerated types and their interpretation by compilers
It seems to me that a lot, if not most, compilers treat enumerated types as int underneath. In C/gcc, enums are compiled to int. In C#/Visual C#, you can change the underlying data type with something ...
4
votes
6
answers
16k
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 ...
1
vote
3
answers
1k
views
How can I mock this architecture?
This is not a very general question, so it may not exactly be appropriate here, but I could sure use a suggestion if you have one:
I have an object containing a dictionary keyed off of an enum, ...
1
vote
2
answers
190
views
Is there a way for Object 1 to call Object 2's enums in a function call?
For example, if I have two classes "Director" and "Follower". I want the Director to tell the follower where to go (ex: follower1.go(direction.LEFT)), and I want the Director to know what directions ...