Do you code `if (foo == true) ` in Java?

There posted the discussion that “Do you code if (foo == true) in Java?” on Qiita, Japanese tech blog.

https://qiita.com/ikemo/items/4f56a283f9e27cf98d81

The auther argues that it’s “No” because its verbose (if (foo) is enough), it may cause typo that if (foo = true) and it would increase the number of steps and run slower.

So, the code should be like this.


if (foo) {
    // do something
}

boolean isBuzz = !obj.getFoo().getBar();
if (!isBuzz) {
    // do something
}

Enter fullscreen mode Exit fullscreen mode

and not


if (foo == true) {
    // do something
}

if (obj.getFoo().getBar() == false) {
    // do something
}

Enter fullscreen mode Exit fullscreen mode

However, there posted many counter arguments that ! operator can be easily overlooked, == true is easy to understand, both ways are acceptable, etc.

Basically, I’m for the author’s opinion. == true doesn’t provide any advantage for readability and simply if (isSuccessful()) is comfortable to pronounce.

How do you think of this?

Thanks.

原文链接:Do you code `if (foo == true) ` in Java?

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容