5 ms·
It's interesting that this is one of the most hated (http://hatepaste.com/paste/f5da3584 http://hatepaste.com/paste/f5da3584): if (someBoolean == true) {
by biot 15y ago
It's interesting that this is one of the most hated (http://hatepaste.com/paste/f5da3584 http://hatepaste.com/paste/f5da3584):
if (someBoolean == true) {
doSomething();
}
I got into the habit of doing this since it's immediately obvious that the value you're comparing is expected to be a boolean and not something like an integer, which could cause subtle bugs later on if what you thought was a boolean gets negated. More commonly, it's of the form:
if (someValue == false) ...
rather than:
if (!someValue) ...
to distinguish this from:
if (someValue == 0) ...
It only takes an extra fraction of a second of typing and increases clarity for the next developer, or for you six months later. Anyone else do this too?
- koenigdavidmj 15y agoIn Java, it's a compile error to pass something not of type `bool' into that expression, so things like if (someBoolean) Are unambiguous.
- biot 15y agoTrue. I suppose this is more of a holdover from my C programming days.
- DrCatbox 15y agoIts far easier to read if (!someBoolean) than if (someBoolean == false) Ofcourse, your example probably applies to JavaScript since anything and everything can be a boolean, or not depending on the phase of the moon. In Java at least if not someBoolean is nice to read, and if (isGood) is nicer than if (isGood == true).
- polyfractal 15y agoI think this is a pretty subjective topic - you can't say that one way is easier to read than any other way (for anyone except yourself). Some people prefer verbosity over brevity, just like some people prefer K&R braces to Allman, or CamelCase to underscoring.
- lurker17 15y agoWow. http://www.paulgraham.com/avg.html http://www.paulgraham.com/avg.html
- biot 15y agoAre you suggesting I should use Lisp, or that I haven't beat the average, or...?
- radicalbyte 15y agoIf you're going to do that, at least go the whole hog and avoid assignment/comparison bugs: if (true == someBoolean) { // code }
- scott_w 15y agoNot usually. However, I do sometimes make explicit comparisons to False or None if I can end up with both, and the behaviour differs depending on what I have. It's probably not the best idea to be doing that, but it's not tripped me up yet ;-)
- ef4 15y agoJust as long as you don't do this: if (someBoolean){ return true; } else { return false; }