您首选的布尔对是什么:1/0 是/否 True/False?

发布于 2024-07-12 00:23:41 字数 133 浏览 8 评论 0原文

  1. 在处理 MySQL 时,我通常使用 BOOLEAN 类型,它相当于 TINYINT(1) 或 1/0
  2. 在我使用的大多数语言中,首选 true/false
  3. 在显示表单时,有时“是/否”更有意义
  1. When dealing with MySQL, I typically use the BOOLEAN type, which is equivalent to TINYINT(1), or 1/0
  2. In most languages I work with, true/false is preferred
  3. When displaying forms, sometimes "Yes / No" makes more sense

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

︶ ̄淡然 2024-07-19 00:23:41
enum Bool 
{ 
    True, 
    False, 
    FileNotFound 
};

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

enum Bool 
{ 
    True, 
    False, 
    FileNotFound 
};

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

你げ笑在眉眼 2024-07-19 00:23:41

在代码中:真/假。

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

In code: true/false.

In the UI: Yes/No or OK/Cancel

阪姬 2024-07-19 00:23:41

哪个更容易阅读?

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

大多数情况下我都会坚持使用 true。

Which is easier to read?

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

I'll stick with true for most cases.

哥,最终变帅啦 2024-07-19 00:23:41

truefalse 对我来说在代码中更有意义 - 我确信部分是由于熟悉。 我怀疑我很快就会习惯。 1 和 0 对我来说确实不起作用。

这个表达

age == 5

考虑一下“It's a test for true-hood” 。 age 的值是 5 吗? 对,是真的。 “是”和“否”对我来说都可以,但问题“年龄的值是 5 吗?”的答案的想法 是“1”对我来说似乎非常违反直觉。 仅仅因为这是真理的典型二进制表示并不意味着它在更高的抽象上是有用的。

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.

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.

晨曦÷微暖 2024-07-19 00:23:41

以下是我遵循的规则...

规则 #1

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

规则#2

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

规则#3

不确定性应该用范围来表达,即“这取决于”,接下来的问题是“取决于什么?”。 我知道开发人员通过将他们可能需要的几乎所有依赖项作为“使用”语句复制并粘贴到项目的每个代码文件中来回答问题。 这太草率了,请费心按字母顺序排列或至少将名称空间分组在一起。

当 bool 还不够时

顺便说一句,C# 中对此有一个有趣的转变,即 Nullable;

你可以写

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

}

“OR”

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

}

,提问者需要先评估你的回答,然后才能知道答案(如果有的话)可能是什么……

bool? answer = RespondToIritatingQuestionWithStyle();

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

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.

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.

When a bool Just Isn't Enough

Incidentally, an interesting twist to this, available in C#, is 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'.");
寂寞美少年 2024-07-19 00:23:41

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

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

静若繁花 2024-07-19 00:23:41

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文