有布尔值的助记技巧吗?

发布于 2024-07-13 16:11:44 字数 365 浏览 11 评论 0原文

我想这对于大多数优秀的1程序员来说都是微不足道的,但我已经习惯使用truefalse2<进行编程/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 false2 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 技术交流群。

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

发布评论

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

评论(11

小姐丶请自重 2024-07-20 16:11:44

助记符是“这有多少真相?” 零整数意味着零真理。 其他任何事情都是非零真理。 :)

The mnemonic is "how much truth is in this?" Zero integer means zero truth. Anything else is nonzero truth. :)

白衬杉格子梦 2024-07-20 16:11:44

我有一位同事,他办公桌旁边的墙上贴了一张便利贴:

假 = 0
正确!= 0

I have a co-worker who simply has a Post-It note on his wall beside his desk:

False = 0
True != 0

软糖 2024-07-20 16:11:44

如果你真的遇到那么多麻烦,我会用语言将其抽象出来。

例如,在 C 中

#define TRUE 1
#define FALSE 0

,无论如何,我都会避免在代码中出现常量。

考虑一下,

if(my_var == TRUE)

而不是,

if(my_var == 1)

尽管如此,你再次需要确保你正在测试正确的东西,

if(my_var != FALSE)

这样会捕获更多的情况。

干杯!

基督教

If you are really having that much trouble with it, I would use the language to abstract it away.

e.g. in C

#define TRUE 1
#define FALSE 0

In general I would avoid having constants lying around in code anyways.

Consider,

if(my_var == TRUE)

as opposed to,

if(my_var == 1)

Though, here again you need to make sure you are testing for the right thing,

if(my_var != FALSE)

will catch more cases.

Cheers!

Christian

只是偏爱你 2024-07-20 16:11:44

你有没有注意到日常用品的电源开关使用圆圈表示关闭,线条表示打开< /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

兮颜 2024-07-20 16:11:44

“哦不!”
(噢==0)

"Oh No!"
(Oh == 0)

复古式 2024-07-20 16:11:44

由于在 shell(sh、bash)中 true 为 0 而 false 为 1,这一事实使情况变得复杂:

$ true
$ echo $?
0
$ false
$ echo $?
1

This is complicated by the fact that in the shell (sh, bash) true is 0 and false is 1:

$ true
$ echo $?
0
$ false
$ echo $?
1
伴我老 2024-07-20 16:11:44

记住“没有 == false”,“有 == true”

Remember "nothing == false", "something == true"

删除会话 2024-07-20 16:11:44

没有助记符 - 如果您有硬件背景,它会变得更加复杂。 但对于程序员来说,只需问一个问题:

是否设置了任何位?

答案要么是真,要么是假,就是结果。 只有 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

动次打次papapa 2024-07-20 16:11:44

将两者映射为OnOff。 我认为大多数程序员会以相同的方式映射这两个集合:1/true 都转到“On”,而 0/false 都转到“Off”。

Map both to On and Off. I think most programmers would map both sets the same way: 1/true both going to 'On', while 0/false both go to 'Off'.

薄凉少年不暖心 2024-07-20 16:11:44

这取决于语言。

  • C( 0 ? "从不发生" : "假")( 1 ? "true" : "从不发生")

  • RubyELisp01 均为 true

  • bash(或 < 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 and 1 are true

  • bash (or cmd.exe): the true command (from coreutils) exits with a 0 status code and the false command exits with a non-zero status code

Many modern popular programming languages have strong C heritage therefore they consider 0 to be false and 1 (or any non-zero numbers) to be true.

Don't use mnemonics for boolean, use your language's idioms to test trueness.

憧憬巴黎街头的黎明 2024-07-20 16:11:44

爱c - 因为你不能增加谎言。 :)

love c - because you can't multiply lies. :)

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