它是坏的明确比较的布尔常量例如如果(二==假)在Java中? [英] Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

查看:111
本文介绍了它是坏的明确比较的布尔常量例如如果(二==假)在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是不好写:

if (b == false) //...

while (b != true) //...

是不是的总是的更好,而不是写:

if (!b) //...

while (!b) //...

presumably在性能没有区别(或有),但你如何权衡两者之间的明确性,简洁性,条理清晰,可读性等?

Presumably there is no difference in performance (or is there?), but how do you weigh the explicitness, the conciseness, the clarity, the readability, etc between the two?

要限制的主观性,我也AP preciate从权威的编码风格准则的任何报价超过它总是preferable或当使用。

To limit the subjectivity, I'd also appreciate any quotes from authoritative coding style guidelines over which is always preferable or which to use when.

注意的:变量名 B 只是用来作为一个例子,翼

Note: the variable name b is just used as an example, ala foo and bar.

推荐答案

它不一定是坏事,它只是多余的。此外,实际的变量名重了很多。我会$如对$ PFER 如果(userIsAllowedToLogin)上方如果(B)或更糟的 IF(标志)

It's not necessarily bad, it's just superfluous. Also, the actual variable name weights a lot. I would prefer for example if (userIsAllowedToLogin) above if (b) or even worse if (flag).

至于性能问题,编译器优化它拿走的任何方式。

As to the performance concern, the compiler optimizes it away at any way.

更新:作为权威来源,我无法找到明确的东西在的孙编码约定的,但至少 Checkstyle的有一个<一个href=\"http://checkstyle.sourceforge.net/config_coding.html#SimplifyBooleanEx$p$pssion\"><$c$c>SimplifyBooleanEx$p$pssion模块,它会警告有关。

Update: as to the authoritative sources, I can't find something explicitly in the Sun Coding Conventions, but at least Checkstyle has a SimplifyBooleanExpression module which would warn about that.

这篇关于它是坏的明确比较的布尔常量例如如果(二==假)在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆