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.
...
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 ...
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".
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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/...
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 ...
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 ...
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 ...
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), ...
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 ...
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 ...
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/...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
enum × 134java × 42
c# × 26
design-patterns × 15
design × 12
c++ × 12
database × 9
database-design × 9
object-oriented × 8
programming-practices × 7
refactoring × 7
switch-statement × 7
python × 5
api × 5
.net × 4
relational-database × 4
object-oriented-design × 3
c × 3
data-structures × 3
interfaces × 3
inheritance × 3
clean-code × 3
class × 3
anti-patterns × 3
architecture × 2