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
  • 62.4k
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.6k
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,472
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,348
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
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,390
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,981
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.9k
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

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 special ...
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,586
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.6k
6 votes

Java Exception Error Enumerations Anti-pattern

You haven't talked about proposed alternatives to this approach so I'll answer in generalities that may not be relevant to you in a particular situation. Usually, I would expect something like this ...
JimmyJames's user avatar
  • 31.1k
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
  • 82.3k
5 votes
Accepted

How to understand and design functions with enumeration arguments that work in tandem with bitwise operators in C++ and Qt?

Your understanding is pretty good. To answer your questions: Yes, it the function will receive 0x0000 0000 0000 0000 0000 1010 as an argument. The significance of the decimal number 10 in binary is ...
user1118321's user avatar
  • 4,981
5 votes
Accepted

How to add some data to an Enum in Python

There are a couple different ways you can build that information directly into the enum definitions: use the limit values instead of the string for the enum value; add the limit values to the string ...
Ethan Furman's user avatar
4 votes

Enums in java switch-statements and completeness

With Java 17+ you can use the new switch syntax to ensure that you covered all cases: private String getMyEnumClassDescription(MyEnumClass m) { return switch (m) { case A -> "the ...
Krzysztof's user avatar
  • 141
4 votes
Accepted

Using extraneous enum members for bounds/validity checking

Enums can only be out of range if you do arithmetic on them - adding 1 to get to the next one, etc. This is the root of any problems you could encounter with out-of-bounds. The solution is of course ...
Aganju's user avatar
  • 1,473
4 votes
Accepted

Best pattern to solve problem where objects only differ in one attribute

How could this problem be solved without breaking the concept of clean OOP? This is not an OOP problem. This is a data structure problem. I know this because there is no behavior associated with this ...
candied_orange's user avatar
4 votes

What if there is specific logic for one particular value of an enumeration class?

Basically both of the articles you reference are wrong. You should use enums where you have an enum. You should also have change control on your database. If you bypass your change control processes ...
Ewan's user avatar
  • 84.6k
4 votes
Accepted

How to gradually refactor a Map with string keys to use enum keys?

If you really need to introduce this gradually, you will temporarily have two kind of maps in your program: one Map<String, Object> type, and one Map<MyEnum, Object> type. A conversion ...
Doc Brown's user avatar
  • 221k
4 votes

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

You're just talking about the integral value here. I'd avoid using the word "index" as it doesn't necessarily follow that it will be an index in the way you describe. You can set any values for ...
Belgian Dog's user avatar
4 votes
Accepted

Name of this enum-based design pattern to get the type

This pattern is called “type discriminator”. It was very useful before OOP languages, for example to simulate polymorphic types with the help of discriminated unions. It is still heavily used and ...
Christophe's user avatar
  • 82.3k

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