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
1
vote
4answers
123 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 Evil" ...
1
vote
1answer
200 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
1answer
168 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
4answers
197 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
0answers
62 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
3answers
99 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
1answer
131 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
1answer
66 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
2answers
101 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 ...
3
votes
4answers
372 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
1answer
202 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
1answer
151 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
1answer
286 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. ...
2
votes
1answer
3k 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
1answer
106 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. ...
2
votes
1answer
1k 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
1answer
260 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
3answers
167 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, ...
14
votes
9answers
24k 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
7answers
994 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 ...
0
votes
1answer
130 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' ...
0
votes
3answers
2k 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, ...
4
votes
2answers
342 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
3answers
288 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 ...
12
votes
4answers
4k 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
2answers
4k 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"]....
4
votes
2answers
611 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
1answer
2k 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
3answers
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
2answers
494 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
1answer
2k 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
2answers
223 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 (...
1
vote
2answers
3k 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
0answers
162 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
1answer
74 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
3answers
459 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 ...
32
votes
5answers
7k 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
2answers
3k 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
0answers
101 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
2answers
250 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
4answers
179 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
2answers
4k 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 ...
11
votes
1answer
5k 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
1answer
122 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 ...
67
votes
8answers
51k 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
2answers
3k 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
1answer
435 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: ...
1
vote
1answer
492 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 ...
14
votes
6answers
11k 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 ...
1
vote
2answers
394 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 ...