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
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
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,...
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
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 ...
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 ...
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
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'...
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<...
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 ...
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
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;
...
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 ...
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/...
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 ...
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 ...
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....
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 ...
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, ...
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