你首选的布尔对是什么:1/0是/否真/假?

时间:2021-06-04 10:00:58
  1. When dealing with MySQL, I typically use the BOOLEAN type, which is equivalent to TINYINT(1), or 1/0
  2. 在处理MySQL时,我通常使用BOOLEAN类型,它相当于TINYINT(1)或1/0

  3. In most languages I work with, true/false is preferred
  4. 在我使用的大多数语言中,首选true / false

  5. When displaying forms, sometimes "Yes / No" makes more sense
  6. 在显示表单时,有时“是/否”更有意义

7 个解决方案

#1


enum Bool 
{ 
    True, 
    False, 
    FileNotFound 
};

http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx

#2


In code: true/false.

在代码中:true / false。

In the UI: Yes/No or OK/Cancel

在UI中:是/否或确定/取消

#3


true and false makes a lot more sense to me, in code - partly through familiarity, I'm sure. I suspect I'd get used to yes and no pretty quickly. 1 and 0 really doesn't work for me though.

对于我来说,真假对我来说更有意义 - 代码 - 部分是通过熟悉,我敢肯定。我怀疑我已经习惯了,很快就没有了。 1和0真的不适合我。

Consider the expression

考虑一下表达式

age == 5

It's a test for truth-hood. Is the value of age 5? Yes, it's true. Both "yes" and "no" would be fine for me, but the idea that the answer to the question "Is the value of age 5?" is "1" seems pretty counter-intuitive to me. Just because that's the typical binary representation of truth-hood doesn't mean it's a useful one at a higher abstraction.

这是对真相的测试。 5岁的价值是多少?对,是真的。 “是”和“否”对我来说都没问题,但是对“5岁的价值是什么?”这个问题的回答这个想法。是“1”对我来说似乎很反直觉。仅仅因为这是真实引擎的典型二进制表示并不意味着它在更高的抽象中是有用的。

#4


Which is easier to read?

哪个更容易阅读?

while(true) {}
while(yes) {}
while(1) {}

I'll stick with true for most cases.

在大多数情况下,我会坚持认真。

#5


Here are rules I live by...

以下是我的生活规则......

Rule #1

Use well defined constants in the programming languages that you use to communicate with the CPU, i.e., true/false in most modern cases for boolean values. If a database offers a boolean type or some such equivalent, of course it should be used.

在用于与CPU通信的编程语言中使用定义良好的常量,即在大多数现代情况下为布尔值的真/假。如果数据库提供布尔类型或某些等效类型,当然应该使用它。

Rule #2

Interact with users of your software by using their preferred language and idiom, i.e., Yes/No questions should offer Yes/No (or perhaps an alternative to No, such as Cancel).

通过使用他们的首选语言和习语与您的软件用户交互,即,是/否问题应该提供是/否(或者可能是否的替代,例如取消)。

Rule #3

Uncertainty should be expressed in terms of scope, i.e., "that depends", which will be followed up by the question "on what?". I know developers who answer the question by copying and pasting just about every dependency they may need into every code file of a project as a 'using' statement. That's just sloppy, and please bother to alphabetize or at least group namespaces together.

不确定性应以范围表示,即“依赖于”,后面将跟着“关于什么?”的问题。我知道开发人员通过将他们可能需要的每个依赖项复制并粘贴到项目的每个代码文件中作为“using”语句来回答问题。这只是草率,请麻烦按字母顺序或至少将名称空间组合在一起。

When a bool Just Isn't Enough

Incidentally, an interesting twist to this, available in C#, is Nullable;

顺便提一下,C#中可用的一个有趣的转折是Nullable;

The you can write

你可以写

Nullable<bool> RespondToIritatingQuestion()
{
    return new Nullable<bool>();

}

OR

bool? RespondToIritatingQuestionWithSytle()
{
    return new bool?();

}

and the questioner would need to evaluate your response before even knowing what the answer, if there is one, might be...

提问者在知道答案(如果有的话)之前需要评估你的答案,可能......

bool? answer = RespondToIritatingQuestionWithStyle();

if (answer.HasValue)
    Trace.WriteLine("The bloke responded with " + answer.Value.ToString());
else
    Trace.WriteLine("The bloke responded with 'depends'.");

#6


1 or 0 for SQL. SQL has a boolean type for a reason. Also, in very large databases, it can effect performance.

SQL为1或0。 SQL有一个布尔类型是有原因的。此外,在非常大的数据库中,它可以影响性能。

#7


I use booleans for true/false fields in databases. Some people use ENUM('true', 'false'), but thats not my preference. For programming languages, I always use true/false, even if setting it to 0 or 1 will work. And if the form requires 'yes'/'no', I still use booleans to represent the values, but display them as strings that are more logical.

我在数据库中使用布尔值来表示真/假字段。有些人使用ENUM('true','false'),但这不是我的偏好。对于编程语言,我总是使用true / false,即使将其设置为0或1也可以。如果表单需要“是”/“否”,我仍然使用布尔值来表示值,但将它们显示为更符合逻辑的字符串。

#1


enum Bool 
{ 
    True, 
    False, 
    FileNotFound 
};

http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx

#2


In code: true/false.

在代码中:true / false。

In the UI: Yes/No or OK/Cancel

在UI中:是/否或确定/取消

#3


true and false makes a lot more sense to me, in code - partly through familiarity, I'm sure. I suspect I'd get used to yes and no pretty quickly. 1 and 0 really doesn't work for me though.

对于我来说,真假对我来说更有意义 - 代码 - 部分是通过熟悉,我敢肯定。我怀疑我已经习惯了,很快就没有了。 1和0真的不适合我。

Consider the expression

考虑一下表达式

age == 5

It's a test for truth-hood. Is the value of age 5? Yes, it's true. Both "yes" and "no" would be fine for me, but the idea that the answer to the question "Is the value of age 5?" is "1" seems pretty counter-intuitive to me. Just because that's the typical binary representation of truth-hood doesn't mean it's a useful one at a higher abstraction.

这是对真相的测试。 5岁的价值是多少?对,是真的。 “是”和“否”对我来说都没问题,但是对“5岁的价值是什么?”这个问题的回答这个想法。是“1”对我来说似乎很反直觉。仅仅因为这是真实引擎的典型二进制表示并不意味着它在更高的抽象中是有用的。

#4


Which is easier to read?

哪个更容易阅读?

while(true) {}
while(yes) {}
while(1) {}

I'll stick with true for most cases.

在大多数情况下,我会坚持认真。

#5


Here are rules I live by...

以下是我的生活规则......

Rule #1

Use well defined constants in the programming languages that you use to communicate with the CPU, i.e., true/false in most modern cases for boolean values. If a database offers a boolean type or some such equivalent, of course it should be used.

在用于与CPU通信的编程语言中使用定义良好的常量,即在大多数现代情况下为布尔值的真/假。如果数据库提供布尔类型或某些等效类型,当然应该使用它。

Rule #2

Interact with users of your software by using their preferred language and idiom, i.e., Yes/No questions should offer Yes/No (or perhaps an alternative to No, such as Cancel).

通过使用他们的首选语言和习语与您的软件用户交互,即,是/否问题应该提供是/否(或者可能是否的替代,例如取消)。

Rule #3

Uncertainty should be expressed in terms of scope, i.e., "that depends", which will be followed up by the question "on what?". I know developers who answer the question by copying and pasting just about every dependency they may need into every code file of a project as a 'using' statement. That's just sloppy, and please bother to alphabetize or at least group namespaces together.

不确定性应以范围表示,即“依赖于”,后面将跟着“关于什么?”的问题。我知道开发人员通过将他们可能需要的每个依赖项复制并粘贴到项目的每个代码文件中作为“using”语句来回答问题。这只是草率,请麻烦按字母顺序或至少将名称空间组合在一起。

When a bool Just Isn't Enough

Incidentally, an interesting twist to this, available in C#, is Nullable;

顺便提一下,C#中可用的一个有趣的转折是Nullable;

The you can write

你可以写

Nullable<bool> RespondToIritatingQuestion()
{
    return new Nullable<bool>();

}

OR

bool? RespondToIritatingQuestionWithSytle()
{
    return new bool?();

}

and the questioner would need to evaluate your response before even knowing what the answer, if there is one, might be...

提问者在知道答案(如果有的话)之前需要评估你的答案,可能......

bool? answer = RespondToIritatingQuestionWithStyle();

if (answer.HasValue)
    Trace.WriteLine("The bloke responded with " + answer.Value.ToString());
else
    Trace.WriteLine("The bloke responded with 'depends'.");

#6


1 or 0 for SQL. SQL has a boolean type for a reason. Also, in very large databases, it can effect performance.

SQL为1或0。 SQL有一个布尔类型是有原因的。此外,在非常大的数据库中,它可以影响性能。

#7


I use booleans for true/false fields in databases. Some people use ENUM('true', 'false'), but thats not my preference. For programming languages, I always use true/false, even if setting it to 0 or 1 will work. And if the form requires 'yes'/'no', I still use booleans to represent the values, but display them as strings that are more logical.

我在数据库中使用布尔值来表示真/假字段。有些人使用ENUM('true','false'),但这不是我的偏好。对于编程语言,我总是使用true / false,即使将其设置为0或1也可以。如果表单需要“是”/“否”,我仍然使用布尔值来表示值,但将它们显示为更符合逻辑的字符串。