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.
暂无评论内容