Or are we all sticking to our taught "&&, ||, !" way?
或者我们都坚持我们的教学“&&,||,!”办法?
Any thoughts in why we should use one or the other?
我们为什么要使用其中一个的任何想法?
I'm just wondering because several answers state thate code should be as natural as possible, but I haven't seen a lot of code with "and, or, not" while this is more natural.
我只是想知道,因为有几个答案说明代码应该尽可能自然,但是我没有看到很多带有“and,or,not”的代码,而这更自然。
13 个解决方案
#1
Those were not supported in the old days. And even now you need to give a special switch to some compilers to enable these keywords. That's probably because old code base may have had some functions || variables named "and" "or" "not".
过去,这些都没有得到支持。即使是现在,您仍需要对某些编译器进行特殊切换以启用这些关键字。这可能是因为旧的代码库可能有一些函数||名为“和”或“不”的变量。
#2
I like the idea of the not
operator because it is more visible than the !
operator. For example:
我喜欢not运算符的想法,因为它比!更明显!运营商。例如:
if (!foo.bar()) { ... }
if (not foo.bar()) { ... }
I suggest that the second one is more visible and readable. I don't think the same argument necessarily applies to the and
and or
forms, though.
我建议第二个更明显和可读。不过,我不认为同样的论点必然适用于和/或形式。
#3
"What's in a name? That which we call &&, || or ! By any other name would smell as sweet."
“名字里面有什么?我们称之为&&,||或者!任何其他名字都会闻到它的味道。”
In other words, natural depends on what you are used to.
换句话说,自然取决于你习惯的东西。
#4
One problem with using them (for me anyway) is that in MSVC you have to include iso646.h
or use the (mostly unusable) /Za switch.
使用它们的一个问题(无论如何)是在MSVC中你必须包括iso646.h或使用(主要是不可用的)/ Za开关。
The main problem I have with them is the Catch-22 that they're not commonly used, so they require my brain to actively process the meaning, where the old-fashioned operators are more or less ingrained (kind of like the difference between reading a learned language vs. your native language).
我遇到的主要问题是它们不常用的Catch-22,所以它们需要我的大脑积极处理意义,老式的操作者或多或少根深蒂固(有点像阅读之间的区别学习语言与母语相比。
Though I'm sure I'd overcome that issue if their use became more universal. If that happened, then I'd have the problem that some boolean operators have keywords while others don't, so if alternate keywords were used, you might see expressions like:
虽然我确信如果他们的使用变得更加普遍,我会克服这个问题。如果发生了这种情况,那么我就会遇到一些布尔运算符有关键字但有些没有关键字的问题,所以如果使用了替代关键字,你可能会看到如下表达式:
if ((x not_eq y) and (y == z) or (z <= something)) {...}
when it seems to me they should have alternate tokens for all the (at least comparison) operators:
在我看来,他们应该为所有(至少比较)运营商提供替代令牌:
if ((x not_eq y) and (y eq z) or (z lt_eq something)) {...}
This is because the reason the alternate keywords (and digraphs and trigraphs) were provided was not to make the expressions more readable - it was because historically there have been (and maybe still are) keyboards and/or codepages in some localities that do not have certain punctuation characters. For example, the invariant part of the ISO 646 codepage (surprise) is missing the '|
', '^
' and '~
' characters among others.
这是因为提供备用关键字(以及有向图和三角形)的原因不是为了使表达式更具可读性 - 这是因为历史上某些地方已经存在(可能仍然是)键盘和/或代码页而没有某些标点字符。例如,ISO 646代码页(惊讶)的不变部分缺少“|”,“^”和“〜”等字符。
#5
Although I've been programming C++ from quite some time, I did not know that the keywords "and" "or" and "not" were allowed, and I've never seen it used.
虽然我从很长一段时间以来一直在编写C ++,但我不知道关键字“and”“或”和“not”是允许的,我从未见过它。
I searched through my C++ book, and I found a small section mentioning alternative representation for the normal operators "&&", "||" and "!", where it explains those are available for people with non-standard keyboards that do not have the "&!|" symbols.
我搜索了我的C ++书籍,我发现了一小部分提到正常运算符的替代表示“&&”,“||”和“!”,它解释了那些没有“&!|”的非标准键盘的用户符号。
A bit like trigraphs in C.
有点像C中的三字母。
Basically, I would be confused by their use, and I think I would not be the only one. Using a representation which is non-standard, should really have a good reason to be used. And if used, it should be used consistently in the code, and described in the coding standard.
基本上,我会对它们的使用感到困惑,我想我不会是唯一的。使用非标准的表示,应该有充分的理由使用。如果使用,它应该在代码中一致地使用,并在编码标准中描述。
#6
The digraph and trigraph operators were actually designed more for systems that didn't carry the standard ASCII character set - such as IBM mainframes (which use EBCDIC). In the olden days of mechanical printers, there was this thing called a "48-character print chain" which, as its name implied, only carried 48 characters. A-Z (uppercase), 0-9 and a handful of symbols. Since one of the missing symbols was an underscore (which rendered as a space), this could make working with languages like C and PL/1 a real fun activity (is this 2 words or one word with an underscore???).
实际上,有向图和三字符运算符的设计更适用于没有标准ASCII字符集的系统 - 例如IBM大型机(使用EBCDIC)。在机械打印机的旧时代,有一种称为“48字符打印链”的东西,顾名思义,它只带有48个字符。 A-Z(大写),0-9和一些符号。由于其中一个缺失的符号是下划线(呈现为空格),这可以使像C和PL / 1这样的语言成为一种真正有趣的活动(这是2个单词还是带有下划线的单词???)。
Conventional C/C++ is coded with the symbols and not the digraphs. Although I have been known to #define "NOT", since it makes the meaning of a boolean expression more obvious, and it's visually harder to miss than a skinny little "!".
传统的C / C ++用符号编码而不是有向图。虽然我已经知道#define“NOT”,因为它使布尔表达式的含义更加明显,并且它比一个瘦小的“!”更难以错过。
#7
I wish I could use || and && in normal speech. People try very hard to misunderstand when I say "and" or "or"...
我希望我能用||和&&在正常的演讲中。当我说“和”或“或”时,人们会非常努力地误解...
#8
I personally like operators to look like operators. It's all maths, and unless you start using "add" and "subtract" operators too it starts to look a little inconsistent.
我个人喜欢运营商看起来像运营商。这是所有数学,除非你开始使用“添加”和“减去”运算符,它开始看起来有点不一致。
I think some languages suit the word-style and some suit the symbols if only because it's what people are used to and it works. If it ain't broke, don't fix it.
我认为有些语言适合单词式,有些适合符号,只因为它是人们习惯的而且有效。如果没有损坏,请不要修理它。
There is also the question of precedence, which seems to be one of the reasons for introducing the new operators, but who can be bothered to learn more rules than they need to?
还有一个优先权问题,这似乎是引入新运营商的原因之一,但谁可能不愿意学习更多的规则而不是他们需要的?
#9
In cases where I program with names directly mapped to the real world, I tend to use 'and' and 'or', for example:
如果我使用直接映射到现实世界的名称编程,我倾向于使用'和'和'或',例如:
if(isMale or isBoy and age < 40){}
#10
It's nice to use 'em in Eclipse+gcc, as they are highlighted. But then, the code doesn't compile with some compilers :-(
在Eclipse + gcc中使用'em是很好的,因为它们被突出显示。但是,代码不能编译一些编译器:-(
#11
I like the idea, but don't use them. I'm so used to the old way that it provides no advantage to me doing it either way. Same holds true for the rest of our group, however, I do have concerns that we might want to switch to help avoid future programmers from stumbling over the old symbols.
我喜欢这个主意,但不要使用它们。我已经习惯了以前的方式,这对我来说没有任何好处。同样适用于我们小组的其他成员,但是,我确实担心我们可能希望转而帮助避免未来的程序员绊倒旧符号。
#12
So to summarize: it's not used a lot because of following combination
总结一下:由于以下组合,它没有被大量使用
- old code where it was not used
- habit (more standard)
- taste (more math-like)
不使用它的旧代码
习惯(更标准)
味道(更像数学)
Thanks for your thoughts
谢谢你的想法
#13
Using these operators is harmful. Notice that and
and or
are logical operators whereas the similar-looking xor
is a bitwise operator. Thus, arguments to and
and or
are normalized to 0
and 1
, whereas those to xor
aren't.
使用这些运算符是有害的。请注意,和和是逻辑运算符,而类似外观的xor是按位运算符。因此,和和的参数归一化为0和1,而xor的参数则不归零。
Imagine something like
想象一下
char *p, *q; // Set somehow
if(p and q) { ... } // Both non-NULL
if(p or q) { ... } // At least one non-NULL
if(p xor q) { ... } // Exactly one non-NULL
Bzzzt, you have a bug. In the last case you're testing whether at least one of the bits in the pointers is different, which probably isn't what you thought you were doing because then you would have written p != q
.
Bzzzt,你有一个bug。在最后一种情况下,您正在测试指针中的至少一个位是否不同,这可能不是您认为的那样,因为那时您将编写p!= q。
This example is not hypothetical. I was working together with a student one time and he was fond of these literate operators. His code failed every now and then for reasons that he couldn't explain. When he asked me, I could zero in on the problem because I knew that C++ doesn't have a logical xor operator, and that line struck me as very odd.
这个例子不是假设的。我和一个学生一起工作,他喜欢这些有文化的操作员。他的代码时不时地失败,原因是他无法解释。当他问我时,我可以解决这个问题,因为我知道C ++没有逻辑xor运算符,而且这条线让我觉得很奇怪。
BTW the way to write a logical xor in C++ is
BTW在C ++中编写逻辑xor的方法是
!a != !b
#1
Those were not supported in the old days. And even now you need to give a special switch to some compilers to enable these keywords. That's probably because old code base may have had some functions || variables named "and" "or" "not".
过去,这些都没有得到支持。即使是现在,您仍需要对某些编译器进行特殊切换以启用这些关键字。这可能是因为旧的代码库可能有一些函数||名为“和”或“不”的变量。
#2
I like the idea of the not
operator because it is more visible than the !
operator. For example:
我喜欢not运算符的想法,因为它比!更明显!运营商。例如:
if (!foo.bar()) { ... }
if (not foo.bar()) { ... }
I suggest that the second one is more visible and readable. I don't think the same argument necessarily applies to the and
and or
forms, though.
我建议第二个更明显和可读。不过,我不认为同样的论点必然适用于和/或形式。
#3
"What's in a name? That which we call &&, || or ! By any other name would smell as sweet."
“名字里面有什么?我们称之为&&,||或者!任何其他名字都会闻到它的味道。”
In other words, natural depends on what you are used to.
换句话说,自然取决于你习惯的东西。
#4
One problem with using them (for me anyway) is that in MSVC you have to include iso646.h
or use the (mostly unusable) /Za switch.
使用它们的一个问题(无论如何)是在MSVC中你必须包括iso646.h或使用(主要是不可用的)/ Za开关。
The main problem I have with them is the Catch-22 that they're not commonly used, so they require my brain to actively process the meaning, where the old-fashioned operators are more or less ingrained (kind of like the difference between reading a learned language vs. your native language).
我遇到的主要问题是它们不常用的Catch-22,所以它们需要我的大脑积极处理意义,老式的操作者或多或少根深蒂固(有点像阅读之间的区别学习语言与母语相比。
Though I'm sure I'd overcome that issue if their use became more universal. If that happened, then I'd have the problem that some boolean operators have keywords while others don't, so if alternate keywords were used, you might see expressions like:
虽然我确信如果他们的使用变得更加普遍,我会克服这个问题。如果发生了这种情况,那么我就会遇到一些布尔运算符有关键字但有些没有关键字的问题,所以如果使用了替代关键字,你可能会看到如下表达式:
if ((x not_eq y) and (y == z) or (z <= something)) {...}
when it seems to me they should have alternate tokens for all the (at least comparison) operators:
在我看来,他们应该为所有(至少比较)运营商提供替代令牌:
if ((x not_eq y) and (y eq z) or (z lt_eq something)) {...}
This is because the reason the alternate keywords (and digraphs and trigraphs) were provided was not to make the expressions more readable - it was because historically there have been (and maybe still are) keyboards and/or codepages in some localities that do not have certain punctuation characters. For example, the invariant part of the ISO 646 codepage (surprise) is missing the '|
', '^
' and '~
' characters among others.
这是因为提供备用关键字(以及有向图和三角形)的原因不是为了使表达式更具可读性 - 这是因为历史上某些地方已经存在(可能仍然是)键盘和/或代码页而没有某些标点字符。例如,ISO 646代码页(惊讶)的不变部分缺少“|”,“^”和“〜”等字符。
#5
Although I've been programming C++ from quite some time, I did not know that the keywords "and" "or" and "not" were allowed, and I've never seen it used.
虽然我从很长一段时间以来一直在编写C ++,但我不知道关键字“and”“或”和“not”是允许的,我从未见过它。
I searched through my C++ book, and I found a small section mentioning alternative representation for the normal operators "&&", "||" and "!", where it explains those are available for people with non-standard keyboards that do not have the "&!|" symbols.
我搜索了我的C ++书籍,我发现了一小部分提到正常运算符的替代表示“&&”,“||”和“!”,它解释了那些没有“&!|”的非标准键盘的用户符号。
A bit like trigraphs in C.
有点像C中的三字母。
Basically, I would be confused by their use, and I think I would not be the only one. Using a representation which is non-standard, should really have a good reason to be used. And if used, it should be used consistently in the code, and described in the coding standard.
基本上,我会对它们的使用感到困惑,我想我不会是唯一的。使用非标准的表示,应该有充分的理由使用。如果使用,它应该在代码中一致地使用,并在编码标准中描述。
#6
The digraph and trigraph operators were actually designed more for systems that didn't carry the standard ASCII character set - such as IBM mainframes (which use EBCDIC). In the olden days of mechanical printers, there was this thing called a "48-character print chain" which, as its name implied, only carried 48 characters. A-Z (uppercase), 0-9 and a handful of symbols. Since one of the missing symbols was an underscore (which rendered as a space), this could make working with languages like C and PL/1 a real fun activity (is this 2 words or one word with an underscore???).
实际上,有向图和三字符运算符的设计更适用于没有标准ASCII字符集的系统 - 例如IBM大型机(使用EBCDIC)。在机械打印机的旧时代,有一种称为“48字符打印链”的东西,顾名思义,它只带有48个字符。 A-Z(大写),0-9和一些符号。由于其中一个缺失的符号是下划线(呈现为空格),这可以使像C和PL / 1这样的语言成为一种真正有趣的活动(这是2个单词还是带有下划线的单词???)。
Conventional C/C++ is coded with the symbols and not the digraphs. Although I have been known to #define "NOT", since it makes the meaning of a boolean expression more obvious, and it's visually harder to miss than a skinny little "!".
传统的C / C ++用符号编码而不是有向图。虽然我已经知道#define“NOT”,因为它使布尔表达式的含义更加明显,并且它比一个瘦小的“!”更难以错过。
#7
I wish I could use || and && in normal speech. People try very hard to misunderstand when I say "and" or "or"...
我希望我能用||和&&在正常的演讲中。当我说“和”或“或”时,人们会非常努力地误解...
#8
I personally like operators to look like operators. It's all maths, and unless you start using "add" and "subtract" operators too it starts to look a little inconsistent.
我个人喜欢运营商看起来像运营商。这是所有数学,除非你开始使用“添加”和“减去”运算符,它开始看起来有点不一致。
I think some languages suit the word-style and some suit the symbols if only because it's what people are used to and it works. If it ain't broke, don't fix it.
我认为有些语言适合单词式,有些适合符号,只因为它是人们习惯的而且有效。如果没有损坏,请不要修理它。
There is also the question of precedence, which seems to be one of the reasons for introducing the new operators, but who can be bothered to learn more rules than they need to?
还有一个优先权问题,这似乎是引入新运营商的原因之一,但谁可能不愿意学习更多的规则而不是他们需要的?
#9
In cases where I program with names directly mapped to the real world, I tend to use 'and' and 'or', for example:
如果我使用直接映射到现实世界的名称编程,我倾向于使用'和'和'或',例如:
if(isMale or isBoy and age < 40){}
#10
It's nice to use 'em in Eclipse+gcc, as they are highlighted. But then, the code doesn't compile with some compilers :-(
在Eclipse + gcc中使用'em是很好的,因为它们被突出显示。但是,代码不能编译一些编译器:-(
#11
I like the idea, but don't use them. I'm so used to the old way that it provides no advantage to me doing it either way. Same holds true for the rest of our group, however, I do have concerns that we might want to switch to help avoid future programmers from stumbling over the old symbols.
我喜欢这个主意,但不要使用它们。我已经习惯了以前的方式,这对我来说没有任何好处。同样适用于我们小组的其他成员,但是,我确实担心我们可能希望转而帮助避免未来的程序员绊倒旧符号。
#12
So to summarize: it's not used a lot because of following combination
总结一下:由于以下组合,它没有被大量使用
- old code where it was not used
- habit (more standard)
- taste (more math-like)
不使用它的旧代码
习惯(更标准)
味道(更像数学)
Thanks for your thoughts
谢谢你的想法
#13
Using these operators is harmful. Notice that and
and or
are logical operators whereas the similar-looking xor
is a bitwise operator. Thus, arguments to and
and or
are normalized to 0
and 1
, whereas those to xor
aren't.
使用这些运算符是有害的。请注意,和和是逻辑运算符,而类似外观的xor是按位运算符。因此,和和的参数归一化为0和1,而xor的参数则不归零。
Imagine something like
想象一下
char *p, *q; // Set somehow
if(p and q) { ... } // Both non-NULL
if(p or q) { ... } // At least one non-NULL
if(p xor q) { ... } // Exactly one non-NULL
Bzzzt, you have a bug. In the last case you're testing whether at least one of the bits in the pointers is different, which probably isn't what you thought you were doing because then you would have written p != q
.
Bzzzt,你有一个bug。在最后一种情况下,您正在测试指针中的至少一个位是否不同,这可能不是您认为的那样,因为那时您将编写p!= q。
This example is not hypothetical. I was working together with a student one time and he was fond of these literate operators. His code failed every now and then for reasons that he couldn't explain. When he asked me, I could zero in on the problem because I knew that C++ doesn't have a logical xor operator, and that line struck me as very odd.
这个例子不是假设的。我和一个学生一起工作,他喜欢这些有文化的操作员。他的代码时不时地失败,原因是他无法解释。当他问我时,我可以解决这个问题,因为我知道C ++没有逻辑xor运算符,而且这条线让我觉得很奇怪。
BTW the way to write a logical xor in C++ is
BTW在C ++中编写逻辑xor的方法是
!a != !b