Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good name for an Enum for Yes & No values

Background

In a C# command-line app I'm writing, several of the parameters have "yes" and "no" as the possible values.

I am storing their input using the Enum type shown below.

enum YesNo
{
     Yes,
     No
}

Which is fine - the code works. No problem there.

NOTE: Yes, I could store these as bool (that's how it used to work). My design choice is to be explicit about the Yes/No choice made by the user because they will see this printed in other contexts and I'd like it to be more obvious what the choice was.

My Question

  • It just seems odd to have an enum called "YesNo" - what are some suggestions for better names for an enum for "yes" and "no" values.

So Finally

I asked this question relatively early in StackOverflow's life. It wasn't fake question - I really did have this situation. I just thought it would be nice to use it see what the community would do. Because it is, I admit a somewhat odd question.

First, thanks to all who spent the time replying. I'm trying to pay that back with a thoughtful conclusion.

Comments on the answers

switching to bool. I understand your motivation, but I feel I need to point out that having a binary choice (and by that I mean a choice between any two values - alive/dead, married/unmarried, etc.) is not the same as boolean choice between true and false. We find as programmers switching between yes/no and true/false easy - fair enough. Had my choice in this case been for example "Democrat" or "Replication"" (contrived example, I know) then you can see possibilities for confusion or at least awkwardness. I do think the bool option is valid in this case, but less so in other binary choices.

localization - great point. In my specific case it didn't matter - this was not and is never going to be localized, but for other situations it is something to consider.

more than three options - In fact, later on I had to add a third value called to represent the valid (in my application) condition of a user specifically not making the choice.

There were a lot of good comments, thank you all!

like image 986
namenlos Avatar asked Dec 22 '08 06:12

namenlos


People also ask

What should I name my enum?

Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.

What is an example of enum?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

What is enum short for?

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy.

What is the full name of enum?

The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration is "flag" and the constant // are the values of the flag.


3 Answers

I'd suggest you use a name that indicates the Value which is set to Yes or No.

E.G.


public enum Married
{
    YES,
    NO
}
like image 191
Nikola Stjelja Avatar answered Sep 20 '22 18:09

Nikola Stjelja


You say you don't want to use bool because it will be printed out for the user to see amongst other contents. That suggests the problem isn't in storage but in display. By all means present true/false as Yes/No, but there's no need to create a whole new type for it IMO.

EDIT: In addition to suggesting you don't use an enum in the first place, I'd strongly recommend that if you do use an enum, you change the order or use explicit values. Having Yes=0, No=1 will be really confusing if you ever end up seeing the values as integers.

like image 31
Jon Skeet Avatar answered Sep 20 '22 18:09

Jon Skeet


  • YesNo
  • Choice
  • BinaryChoice

Or just use a boolean.

like image 29
Burkhard Avatar answered Sep 19 '22 18:09

Burkhard