views:

1003

answers:

16

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.
+1  A: 

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?

moogs
See comment to Burkhard. I'd also argue that "user action" is very vague - given *just* the name, what values would you expect to see?
Jon Skeet
It's related to the design. See how he described it :)Also, the name allows for some additional choices in the future.The name should be what the choice is for, not what the choices are.
moogs
Moogs - I understand what you are getting at. Although I'm not sure it helps me in this case (due various reasons relating to the specifics of the app) - I think your suggestion makes sense long term for my code.
namenlos
@moogs: It still seems wrong to me. UserChoice would be better than UserAction (they haven't taken an action) but it still isn't self-explanatory - it doesn't say what *kind* of choice they've made. If you can't guess the kind of values for an enum based on the name, I'd say there's something wrong.
Jon Skeet
@JonSkeet- if he was more specific about what kind of action it was (like SaveDocument or KillProcess) then I could give a more meaningful name. namenlos?
moogs
@moogs: No, because the type of the response (yes or no) doesn't necessarily depend on the type of the question. Even BooleanChoice is more descriptive than "UserAction" - but then I don't think he should be using an enum anyway...
Jon Skeet
(Btw, I still can't see where you got the word action from - I can't see it in the question or in edits to the question. What am I missing?)
Jon Skeet
@jon skeet - ah, sorry to be vauge. He described it as "choice made by the user". It's storing some thing/response already done by the user.Re-reading the comments, perhaps we're the same road. UserChoice and UserAction are both vague to me. He should give more details on the use.
moogs
A: 
  • YesNo
  • Choice
  • BinaryChoice

Or just use a boolean.

Burkhard
That violates normal .NET naming conventions. "E" is not normally used as an enum prefix.
Jon Skeet
Is it not an enum?*enum* YesNo{ Yes, No}
Burkhard
@Burkhard: Yes, it's an enum. No, that doesn't mean it should be prefixed with an "E". It also shouldn't be plural as it's not a flags enumeration.
Jon Skeet
What enums are and have to be prefixed with an "E"?
Burkhard
Um, not sure what your last comment is meant to mean, but my point is that enums in .NET *aren't* conventionally prefixed with an E, whereas all your suggestions are.
Jon Skeet
I misread your first comment. Sorry.
Burkhard
Unfortunately I now can't remove my downvote. Consider it removed *in spirit* though :)
Jon Skeet
+1 to effect Jon Skeet's *in spirit* un-downvote.
P Daddy
A: 

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

Egil
+6  A: 

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.

Ed Swangren
The users of the cmdline-app will not be so technical - I'd rather they see something that reflects what they typed. I'll take care of mapping to bool internally. Using an enum here is just a simple way for me to get the output to look correct for the end-user without too much effort on my part.
namenlos
Managing data in a program and visualizing it to the user are two different things. You can use a boolean value and just translate it to you users on visualization. Since you enum is not multi-language this gives you more choices, too.
BeowulfOF
+5  A: 

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.

Jayden
+27  A: 

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.

Jon Skeet
Your observation is correct - what the enum does for me is just simplifies a very tiny part of the output generation. I could easily create a function called bool_to_yes_no() - but I thought it would be simpler to just write Console.Writeline("paramfoo: {0}", paramfoo )
namenlos
If you restrict your storage mechanism to always be directly presentable to users, you're going to tie yourself in knots elsewhere, I suspect. I really would just translate it where appropriate. As a bonus, it's then easier for i18n...
Jon Skeet
I'd give you 2 points if I could.
smack0007
bool answer = true;string.Format("User enter {0}", answer ? "Yes" : "No");
Sekhat
erm typo: enter = entered
Sekhat
+1  A: 

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.

PolyThinker
+2  A: 

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

yesraaj
+1  A: 

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).

Matthew Flaschen
A: 

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

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

public enum UserWtf
{
    No,
    Yes,
    FileNotFound
}
Jon Skeet
you forgot Brillant ;)
devio
Only Paula would use Brilliant!
Ruben Steins
A: 

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.

smack0007
+3  A: 

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.

Daniel Paull
+2  A: 

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

leppie
A: 

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.

Oleg
+1  A: 

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
}
Nikola Stjelja
A: 

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

Brian Rudolph