Background
In a C# command-line app I'm writing, several of the parameters have "yes" and "no" as the possible values.
在我正在编写的C#命令行应用程序中,有几个参数的“是”和“否”作为可能的值。
I am storing their input using the Enum type shown below.
我使用下面显示的Enum类型存储他们的输入。
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.
注意:是的,我可以将它们存储为bool(这是它以前的工作原理)。我的设计选择是明确用户做出的是/否选择,因为他们会在其他情况下看到这个,我希望它更明显的选择是什么。
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.
有一个名为“YesNo”的枚举似乎很奇怪 - 对于“是”和“否”值的枚举更好的名称有什么建议。
So Finally
I asked this question relatively early in *'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.
切换到布尔。我理解你的动机,但我觉得我需要指出有一个二元选择(并且我的意思是任意两个值之间的选择 - 活着/死亡,已婚/未婚等)与布尔选择之间的选择不同真假。我们发现程序员在yes / no和true / false之间切换容易 - 足够公平。如果我在这种情况下的选择是例如“*党人”或“复制”“(人为的例子,我知道),那么你可以看到混淆或至少尴尬的可能性。我认为bool选项在这种情况下是有效的,但是更少所以在其他二元选择中。
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!
有很多好评,谢谢大家!
17 个解决方案
#1
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
}
#2
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.
你说你不想使用bool,因为它会打印出来供用户查看其他内容。这表明问题不是存储而是显示。通过任何方式将是/否作为是/否,但是没有必要为它创建一个全新的类型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.
编辑:除了建议您不首先使用枚举,我强烈建议如果您使用枚举,您更改顺序或使用显式值。如果您最终将值视为整数,则Yes = 0,No = 1将会非常混乱。
#3
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.注意:是的,我可以将它们存储为bool(这是它以前的工作原理)。我的设计选择是明确用户做出的是/否选择,因为他们会看到这个打印在其他内容中,我希望它更明显的选择是什么。
I fail to see how a "Yes" or "No" is any more "explicit" than a true or false.
我没有看到“是”或“否”如何更“明确”而不是真或假。
#4
ResponseEnum or EResponse or UserResponse depending on your conventions.
ResponseEnum或EResponse或UserResponse取决于您的约定。
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.
我不会仅限于是或否,因为将来您可能希望添加需要不确定响应的功能。
#5
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.
如果你在这里使用枚举的唯一原因是因为你想要向用户显示一个方便的字符串转换,那么当你做更复杂的事情时,你将会非常严重地陷入困境。从视图中分离模型将为您提供良好的服务。阅读MVC和/或MVVM模式。
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.
我也可能会建议一个带有一些自定义属性的简单布尔值来定义要用来代替“true”和“false”的显示字符串。然后,您可以编写自己的字符串方法,以查找自定义属性。
#6
I would not use a enum at all, just roll your own typeconverter and use booleans.
我根本不会使用枚举,只需滚动自己的typeconverter并使用布尔值。
#7
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.
我想YesNo就好了。考虑诸如“MB_OK”和“MB_YESNO”之类的东西......我知道它不是一种类型,但任何不言自明的东西都应该没问题。
#8
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
是否有任何选择除了是/否之外的选项。对于2个选项坚持使用boolean。尝试单独修改显示区域
#9
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.
我想显示bool值有问题。最好创建一个存储boolean的简单包装器,允许您将它们显示为Yes / No或True / False。
#10
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")));
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. :)
同时仍然完全支持本地化。 useFoo当然是一个告诉函数是否使用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).
_是gettext函数的简称(http://www.gnu.org/software/gettext/),可用于C#(http://www.gnu.org/software/automake/manual/gettext/C_0023)的.html)。
#11
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?
用户看到的内容应该不重要。数据模型可能与您向用户呈现的内容大不相同。一个布尔就足够了。将来有可能采取额外行动吗?
#12
(Humour - please don't take this seriously...)
(幽默 - 请不要认真对待......)
I'm surprised no-one's suggested this yet:
我很惊讶没有人建议这样做:
public enum UserWtf
{
No,
Yes,
FileNotFound
}
#13
- YesNo
- Choice
- BinaryChoice
Or just use a boolean.
或者只使用布尔值。
#14
When I have need something like this, I use the word Flag, as in Flag.Yes
, MarriedFlag.No
, etc.
当我需要这样的东西时,我会使用Flag这个词,如Flag.Yes,MarriedFlag.No等。
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.
这有用的示例:您知道今天只有Yes和No值,但您怀疑将来可能会有其他值(如Maybe)。
#15
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.
我没有看到这个枚举的重点,除非除了是和否之外还有其他一些值。这只是一个布尔。并且制作一个枚举只是为了让你不必输入是或否似乎有点愚蠢。
#16
Use a bool, combined with bool.TrueString and bool.FalseString for display purposes.
使用bool,结合bool.TrueString和bool.FalseString用于显示目的。
#17
I use OptionalBinaryFilter for:
我使用OptionalBinaryFilter:
Yes, No, All
是的,不,全部
But if you have only Yes and No, seem more suitable use of Boolean in a member.
但是如果你只有Yes和No,似乎更适合在成员中使用Boolean。
#1
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
}
#2
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.
你说你不想使用bool,因为它会打印出来供用户查看其他内容。这表明问题不是存储而是显示。通过任何方式将是/否作为是/否,但是没有必要为它创建一个全新的类型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.
编辑:除了建议您不首先使用枚举,我强烈建议如果您使用枚举,您更改顺序或使用显式值。如果您最终将值视为整数,则Yes = 0,No = 1将会非常混乱。
#3
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.注意:是的,我可以将它们存储为bool(这是它以前的工作原理)。我的设计选择是明确用户做出的是/否选择,因为他们会看到这个打印在其他内容中,我希望它更明显的选择是什么。
I fail to see how a "Yes" or "No" is any more "explicit" than a true or false.
我没有看到“是”或“否”如何更“明确”而不是真或假。
#4
ResponseEnum or EResponse or UserResponse depending on your conventions.
ResponseEnum或EResponse或UserResponse取决于您的约定。
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.
我不会仅限于是或否,因为将来您可能希望添加需要不确定响应的功能。
#5
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.
如果你在这里使用枚举的唯一原因是因为你想要向用户显示一个方便的字符串转换,那么当你做更复杂的事情时,你将会非常严重地陷入困境。从视图中分离模型将为您提供良好的服务。阅读MVC和/或MVVM模式。
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.
我也可能会建议一个带有一些自定义属性的简单布尔值来定义要用来代替“true”和“false”的显示字符串。然后,您可以编写自己的字符串方法,以查找自定义属性。
#6
I would not use a enum at all, just roll your own typeconverter and use booleans.
我根本不会使用枚举,只需滚动自己的typeconverter并使用布尔值。
#7
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.
我想YesNo就好了。考虑诸如“MB_OK”和“MB_YESNO”之类的东西......我知道它不是一种类型,但任何不言自明的东西都应该没问题。
#8
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
是否有任何选择除了是/否之外的选项。对于2个选项坚持使用boolean。尝试单独修改显示区域
#9
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.
我想显示bool值有问题。最好创建一个存储boolean的简单包装器,允许您将它们显示为Yes / No或True / False。
#10
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")));
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. :)
同时仍然完全支持本地化。 useFoo当然是一个告诉函数是否使用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).
_是gettext函数的简称(http://www.gnu.org/software/gettext/),可用于C#(http://www.gnu.org/software/automake/manual/gettext/C_0023)的.html)。
#11
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?
用户看到的内容应该不重要。数据模型可能与您向用户呈现的内容大不相同。一个布尔就足够了。将来有可能采取额外行动吗?
#12
(Humour - please don't take this seriously...)
(幽默 - 请不要认真对待......)
I'm surprised no-one's suggested this yet:
我很惊讶没有人建议这样做:
public enum UserWtf
{
No,
Yes,
FileNotFound
}
#13
- YesNo
- Choice
- BinaryChoice
Or just use a boolean.
或者只使用布尔值。
#14
When I have need something like this, I use the word Flag, as in Flag.Yes
, MarriedFlag.No
, etc.
当我需要这样的东西时,我会使用Flag这个词,如Flag.Yes,MarriedFlag.No等。
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.
这有用的示例:您知道今天只有Yes和No值,但您怀疑将来可能会有其他值(如Maybe)。
#15
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.
我没有看到这个枚举的重点,除非除了是和否之外还有其他一些值。这只是一个布尔。并且制作一个枚举只是为了让你不必输入是或否似乎有点愚蠢。
#16
Use a bool, combined with bool.TrueString and bool.FalseString for display purposes.
使用bool,结合bool.TrueString和bool.FalseString用于显示目的。
#17
I use OptionalBinaryFilter for:
我使用OptionalBinaryFilter:
Yes, No, All
是的,不,全部
But if you have only Yes and No, seem more suitable use of Boolean in a member.
但是如果你只有Yes和No,似乎更适合在成员中使用Boolean。