Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, July 29, 2016

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

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!

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


EUserAction ?

You described it as some user action. You could also be more specific, though. The name allows for some additional choices in the future. (The name should be what the choice is for, not what the choices are)

However, for your other, subtle question:

because they will see this printed in other contexts and I'd like it to be more obvious what the choice was.

it shouldn't really matter what the user sees. The data model can be very different from what you present to the user. A bool would have sufficed. Is there a possibility for additional actions in the future?

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


  • YesNo
  • Choice
  • BinaryChoice

Or just use a boolean.

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


Positive? Instead of true or false ala boolean, you have whether the end user is positive to the parameter or not.

Answer by Ed S. for What's a good name for an Enum for Yes & No values


I would be confused to see an enum used for a boolean. You say that:

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 contents and I'd like it to be more obvious what the choice was.

I fail to see how a "Yes" or "No" is any more "explicit" than a true or false.

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


ResponseEnum or EResponse or UserResponse depending on your conventions.

I wouldn't limit yourself to only Yes or No as in the future you may want to add functionality that required an Unsure response also.

Answer by Jon Skeet for What's a good name for an Enum for Yes & No values


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.

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


I think YesNo is just fine. Consider things like "MB_OK" and "MB_YESNO" ... I know it's not a type but anything that's self-explanatory should be fine.

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


Is there any possibility for having option other than yes/no.For just 2 option stick with boolean.Try to modify the display area alone

Answer by Matthew Flaschen for What's a good name for an Enum for Yes & No values


I read your updated explanation, but I still feel this is a poor choice. Booleans exist exactly for this purpose. It is your responsibility to ensure that when "they will see this printed in other contents" appropriate text is outputted. This could be as simple as:

Console.WriteLine(_("Using Foo: ") + (useFoo ? _("Yes") : _("No")));

while still having full support for localization. useFoo is of course a parameter telling the function whether it is using foo. :)

The _ is short for the gettext function (http://www.gnu.org/software/gettext/), which is available for C# (http://www.gnu.org/software/automake/manual/gettext/C_0023.html).

Answer by Jon Skeet for What's a good name for an Enum for Yes & No values


(Humour - please don't take this seriously...)

I'm surprised no-one's suggested this yet:

public enum UserWtf  {      No,      Yes,      FileNotFound  }  

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


I don't see the point of this enum unless there were some other values besides Yes and No. That's just a bool. And making an enum just so you don't have to type out yes or no seems kind of silly.

Answer by Daniel Paull for What's a good name for an Enum for Yes & No values


I'd want to call it:

enum Boolean  {       Yes,       No  }  

No, wait, there is already a built in boolean type you can use.

If your only reason for using an enum here is because there is a convenient conversion to/from a string that you want to show the user, then you are going to get bitten very badly down the track as you do more sophisticated things. A separation of model from view will server you well. Read up on MVC and/or MVVM patterns.

I might also suggest that a simple boolean with some custom attributes that define the display strings to use in place of "true" and "false" might suffice here. You can then write your own to/from string methods that look for your custom attributes.

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


I would not use a enum at all, just roll your own typeconverter and use booleans.

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


I guess there is a problem with displaying bool values. It is better to create simple wrapper that stores boolean allowing you to display them as Yes/No or True/False.

Answer by Nikola Stjelja for What's a good name for an Enum for Yes & No values


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  }  

Answer by Brian Rudolph for What's a good name for an Enum for Yes & No values


Use a bool, combined with bool.TrueString and bool.FalseString for display purposes.

Answer by Roy Leban for What's a good name for an Enum for Yes & No values


When I have need something like this, I use the word Flag, as in Flag.Yes, MarriedFlag.No, etc.

Example of when this is useful: you know there are only Yes and No values today but you suspect there might be additional values (like Maybe) in the future.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.