您首选的布尔对是什么:1/0 是/否 True/False?
- 在处理 MySQL 时,我通常使用 BOOLEAN 类型,它相当于 TINYINT(1) 或 1/0
- 在我使用的大多数语言中,首选 true/false
- 在显示表单时,有时“是/否”更有意义
- When dealing with MySQL, I typically use the BOOLEAN type, which is equivalent to TINYINT(1), or 1/0
- In most languages I work with, true/false is preferred
- When displaying forms, sometimes "Yes / No" makes more sense
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
http://thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
在代码中:真/假。
在 UI 中:是/否或确定/取消
In code: true/false.
In the UI: Yes/No or OK/Cancel
哪个更容易阅读?
大多数情况下我都会坚持使用 true。
Which is easier to read?
I'll stick with true for most cases.
true
和false
对我来说在代码中更有意义 - 我确信部分是由于熟悉。 我怀疑我很快就会习惯是
和否
。 1 和 0 对我来说确实不起作用。这个表达
考虑一下“It's a test for true-hood” 。
age
的值是 5 吗? 对,是真的。 “是”和“否”对我来说都可以,但问题“年龄的值是 5 吗?”的答案的想法 是“1”对我来说似乎非常违反直觉。 仅仅因为这是真理的典型二进制表示并不意味着它在更高的抽象上是有用的。true
andfalse
makes a lot more sense to me, in code - partly through familiarity, I'm sure. I suspect I'd get used toyes
andno
pretty quickly. 1 and 0 really doesn't work for me though.Consider the expression
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 ofage
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.以下是我遵循的规则...
规则 #1
在用于与 CPU 通信的编程语言中使用定义良好的常量,即在大多数现代情况下布尔值的 true/false。 如果数据库提供布尔类型或类似的类似类型,那么当然应该使用它。
规则#2
使用软件用户的首选语言和习语与他们进行交互,即“是/否”问题应提供“是/否”(或者可能是“否”的替代方案,例如“取消”)。
规则#3
不确定性应该用范围来表达,即“这取决于”,接下来的问题是“取决于什么?”。 我知道开发人员通过将他们可能需要的几乎所有依赖项作为“使用”语句复制并粘贴到项目的每个代码文件中来回答问题。 这太草率了,请费心按字母顺序排列或至少将名称空间分组在一起。
当 bool 还不够时
顺便说一句,C# 中对此有一个有趣的转变,即 Nullable;
你可以写
“OR”
,提问者需要先评估你的回答,然后才能知道答案(如果有的话)可能是什么……
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
OR
and the questioner would need to evaluate your response before even knowing what the answer, if there is one, might be...
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.
我在数据库中使用布尔值表示真/假字段。 有些人使用
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.