问题描述
我总是写我的布尔前pressions是这样的:
I have always written my boolean expressions like this:
if (!isValid) {
// code
}
但我的新雇主坚持以下样式:
But my new employer insists on the following style:
if (false == isValid) {
// code
}
是一种风格preferred,或标准?
Is one style preferred, or standard?
推荐答案
我preFER的第一个样式,因为它更自然,我读。这是非常不寻常的看到第二个样式。
I prefer the first style because it is more natural for me to read. It's very unusual to see the second style.
原因之一,有些人会在另一个替代preFER第二:
One reason why some people might prefer the second over another alternative:
if (isValid == false) { ... }
是与后者不小心写一个 =
,而不是 ==
,那么你要分配给isValid而不是测试它但与不断首先你会得到一个编译错误。
is that with the latter you accidentally write a single =
instead of ==
then you are assigning to isValid instead of testing it but with the constant first you will get a compile error.
但随着你的第一个建议,这个问题甚至不是一个问题,所以这是另一个原因preFER第一。
But with your first suggestion this issue isn't even a problem, so this is another reason to prefer the first.
这篇关于什么是用Java编写的布尔前pressions的preferred方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!