有布尔值的助记技巧吗?
我想这对于大多数优秀的1程序员来说都是微不足道的,但我已经习惯使用true
和false
2<进行编程/sup> 当我遇到 0 和 1 时,我永远记不起哪个表示 true,哪个表示 false。
有什么建议么?
1好:我当然是指懂 C 的人 :)
2正如您所猜到的,我是一名 Java 开发人员 ;)
I guess this is trivial for most of good1 programmers, but I'm so used to programming using true
and false
2 that when I encounter 0 and 1, I can never remember which one means true and which one means false.
Any suggestions?
1Good: I mean one who knows C, of course :)
2I am a Java developer, as you have guessed ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
助记符是“这有多少真相?” 零整数意味着零真理。 其他任何事情都是非零真理。 :)
The mnemonic is "how much truth is in this?" Zero integer means zero truth. Anything else is nonzero truth. :)
我有一位同事,他办公桌旁边的墙上贴了一张便利贴:
I have a co-worker who simply has a Post-It note on his wall beside his desk:
如果你真的遇到那么多麻烦,我会用语言将其抽象出来。
例如,在 C 中
,无论如何,我都会避免在代码中出现常量。
考虑一下,
而不是,
尽管如此,你再次需要确保你正在测试正确的东西,
这样会捕获更多的情况。
干杯!
基督教
If you are really having that much trouble with it, I would use the language to abstract it away.
e.g. in C
In general I would avoid having constants lying around in code anyways.
Consider,
as opposed to,
Though, here again you need to make sure you are testing for the right thing,
will catch more cases.
Cheers!
Christian
你有没有注意到日常用品的电源开关使用圆圈表示关闭,线条表示打开< /a>?
将它们连接起来并不需要太大的跳跃。
关 = 圆 = 零 = 假
开 = 线 = 一 = 真
Haven't you ever noticed that everyday items' power switches use a circle for off, and a line for on?
It's not much of a jump to link them up.
Off = circle = zero = false
On = line = one = true
“哦不!”
(噢==0)
"Oh No!"
(Oh == 0)
由于在 shell(sh、bash)中 true 为 0 而 false 为 1,这一事实使情况变得复杂:
This is complicated by the fact that in the shell (sh, bash) true is 0 and false is 1:
记住“没有 == false”,“有 == true”
Remember "nothing == false", "something == true"
没有助记符 - 如果您有硬件背景,它会变得更加复杂。 但对于程序员来说,只需问一个问题:
是否设置了任何位?
答案要么是真,要么是假,就是结果。 只有 0(即使是有符号整数)没有设置位。
-亚当
No mnemonic - and it gets even more complex if you come from a hardware background. But for programmers, just ask the question:
Is any bit set?
The answer is either true or false, and is the result. Only 0 (even in signed integers) has no bits set.
-Adam
将两者映射为
On
和Off
。 我认为大多数程序员会以相同的方式映射这两个集合:1/true 都转到“On”,而 0/false 都转到“Off”。Map both to
On
andOff
. I think most programmers would map both sets the same way: 1/true both going to 'On', while 0/false both go to 'Off'.这取决于语言。
C:
( 0 ? "从不发生" : "假")
和( 1 ? "true" : "从不发生")
Ruby、ELisp:
0
和1
均为 truebash(或 < strong>cmd.exe):
true
命令(来自 coreutils)以0
状态代码退出,false
命令退出带有非零状态代码许多现代流行编程语言都具有很强的 C 传统,因此他们认为
0
是为假,1
(或任何非零数字)为真。不要使用布尔值的助记符,使用您语言的习语来测试真实性。
It depends on the language.
C:
( 0 ? "never happens" : "false")
and( 1 ? "true" : "never happens")
Ruby, ELisp: both
0
and1
are truebash (or cmd.exe): the
true
command (from coreutils) exits with a0
status code and thefalse
command exits with a non-zero status codeMany modern popular programming languages have strong C heritage therefore they consider
0
to be false and1
(or any non-zero numbers) to be true.Don't use mnemonics for boolean, use your language's idioms to test trueness.
爱c - 因为你不能增加谎言。 :)
love c - because you can't multiply lies. :)