Skip to main content
43 votes
Accepted

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? No, they are just state. ...
VoiceOfUnreason's user avatar
20 votes

Should one test the values of an enum using unit tests?

You don't test an enum declaration. You may test whether function input/output has the expected enum values. Example: enum Parity { Even, Odd } Parity GetParity(int x) { ... } You don't ...
JacquesB's user avatar
  • 61k
16 votes
Accepted

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

C# enums are a set of named [integral numeric] constants. Each constant in the enum has a numeric value. So the word you are looking for, for the "the index integer of a enum", is just "value".
David Arno's user avatar
  • 39.5k
13 votes

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

Depends on whether your available eras are available to the calling application. Presumably they are so the user can select what they're interested in. If that's the case then it's a front-end issue ...
LoztInSpace's user avatar
  • 1,209
13 votes
Accepted

Semantic Versioning in OpenAPI with string "enum": new values OK?

It's my understanding that you have to increment the major version number if your code introduces breaking changes. If Thing is used strictly for input and you're now accepting a couple of new values,...
Dan Pichelman's user avatar
13 votes

Should one test the values of an enum using unit tests?

If there's a risk that changing the enum will break your code then sure, anything with the [Flags] attribute in C# would be a good case because adding a value between 2 and 4 (3) would be a bitwise 1 ...
Ian's user avatar
  • 5,442
13 votes

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

Enum types are final by design. You can’t extend their values. That means they work great in switch statements that need to staticly know every possible value. Strings don’t care about any of that. ...
candied_orange's user avatar
12 votes

Should one test the values of an enum using unit tests?

No, a test checking that a enum contains all the valid values and nothing more is essentially repeating the enum’s declaration. You would only be testing that the language properly implements the enum ...
jesm00's user avatar
  • 197
11 votes
Accepted

Alternative to using enum

Creating a single-column table in a DB just for team names would be overkill IMO. Create a proper Teams table then. With a key and Name and maybe some other properties that a team may have. This can ...
devnull's user avatar
  • 3,055
10 votes
Accepted

Is it wrong to use flags for "grouping" enums?

This is definitely abusing enums and flags! It might work for you, but anybody else reading the code is going to be mightily confused. If I understand correctly, you have a hierarchical ...
JacquesB's user avatar
  • 61k
9 votes
Accepted

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

One of the things to keep in mind about enum.Enum is that any non-descriptor attribute is converted into an Enum member -- so property and functions/methods are not (converted). This means you can do ...
Ethan Furman's user avatar
8 votes

When are enums NOT a code smell?

Enums are generally okay, they serve a meaningful purpose to represent a data type that can only take a limited number of concrete, possible values. The two major problems with enums are: They are ...
Mecki's user avatar
  • 2,380
8 votes
Accepted

Few unusual C/C++ declarations

But nowhere in the code the actual Enum had been defined by something like EnumKeys test; For the enumeration, there may never be any declaration of a variable of that type, but it may be used for ...
user1118321's user avatar
  • 4,991
7 votes

Is it wrong to use flags for "grouping" enums?

I find this approach easy enough to read and understand. IMHO, it is not something to get confused about. That being said, I have some concerns about this approach: My main reservation is that there'...
Nikita B's user avatar
  • 299
7 votes

Using scoped enums for bit flags in C++

You can define type-safe enum flags in C++11 by using std::enable_if. This is a rudimentary implementation that may be missing some things: template<typename Enum, bool IsEnum = std::is_enum<...
rubenvb's user avatar
  • 580
7 votes

Semantic Versioning in OpenAPI with string "enum": new values OK?

At the end of the day, the rules and regulations are there to enforce an idea, which is that any change that is likely to cause clients to break is accompanied by a major version change. This is the ...
Jules's user avatar
  • 17.8k
7 votes
Accepted

Enum or reference table when dealing with maps

There's nothing wrong with having an enum column on your database or using enums in your program. But don't use both! The point of enums is to limit possibilities and therefore simplify handling and/...
Neil's user avatar
  • 22.8k
7 votes

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

Enums are intended for things that are unlikely to change. For example, a traffic light has three colors: red, green and yellow. That would be a good candidate for an enum. Even if you included ...
Robert Harvey's user avatar
7 votes
Accepted

Loops for enumerated types

Let's say for some reason you want to iterate over all countries. "That's easy", you say: for (i = Country_China; i <= Country_Usa; i++) { // Do stuff } and you write that loop ...
Philip Kendall's user avatar
6 votes
Accepted

Is it a good practice to write an enum as a class file in java?

Yes, it absolutely is. Its the same as with usual classes and polymorphism: // this code in approx. 100 places int flowerValue; switch(flower) { case Flower.Jasmine: flowerValue = 1; break; ...
marstato's user avatar
  • 4,628
6 votes

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

If no eras provided in the input, then this book is considered to cover all eras. This, and also having a special 'ALL' case, are bad IMHO - your APIs should be explicit where possible. Having ...
GoatInTheMachine's user avatar
6 votes
Accepted

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

Change your code to use the enum throughout - this is a prime example of where enums shine. If you continue to use a string, you'll have validations strewn throughout your code (which is a Bad Thing), ...
mmathis's user avatar
  • 5,508
6 votes
Accepted

Optimizing a string to enum converter

As already hinted at in the answer to the first question, a table might be a good idea, even if only mapping from string to enum. The code would be a bit more readable, even though not necessarily ...
Hans-Martin Mosner's user avatar
6 votes

Optimizing a string to enum converter

First thought that comes to mind is skepticism that this is a bottleneck in your program's execution. Most arduino programs spend most of their time asleep, so you should have plenty of leftover CPU ...
whatsisname's user avatar
  • 27.7k
6 votes
Accepted

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

Some APIs are directly addressed by the user and require a format and data type that is available on the user level. A good example would be URLs. This is also flexible and easily extendable (if pages/...
Martin Maat's user avatar
  • 18.5k
5 votes

Is it wrong to use flags for "grouping" enums?

TL;DR Scroll to the very bottom. From what I see, you are implementing a new language on top of C#. The enums seems to denote the type of an identifier (or, anything that has a name and that appears ...
rwong's user avatar
  • 17k
5 votes
Accepted

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

Why not its own file within the namespace it belongs? For example, if I'm working on Legal Matters in my Domain project and I have an enum of MatterStatus, I'd place the enum with a MatterStatus.cs ...
jleach's user avatar
  • 2,682
5 votes
Accepted

Static per-enum data: constructor, set in initialiser or override getter?

One other option would be to use a second enum and a varargs constructor, along with an EnumSet: enum JavaType { INTEGER(TypeProperty.PRIMITIVE, TypeProperty.NUMERIC), BOOLEAN(TypeProperty....
Pokechu22's user avatar
  • 167
5 votes
Accepted

C# is it better to use string or an enum as a key?

I would generally recommend using enums in this situation. Reasons: It defines very clearly what is allowed and what is not allowed in the structures that are keyed with them, if you define the enum ...
FrustratedWithFormsDesigner's user avatar
5 votes

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

Definitively, an enum could be used for representing the state in a single variable and avoid some repetitions. Take the following definition: public enum State {Starting = 0, Started=1, ...
Christophe's user avatar
  • 80.6k

Only top scored, non community-wiki answers of a minimum length are eligible