4 ms·
Responding to GP; I think it's a tiny bit clearer with `== true`, but I don't feel particularly strongly about it. I wrote this code without caring too much abo
by SeoxyS 12y ago
Responding to GP; I think it's a tiny bit clearer with `== true`, but I don't feel particularly strongly about it. I wrote this code without caring too much about style and cleanliness.
However, I don't agree that the shortest code is always the cleanest. In JS for example, I've found that wrapping every conditional body in braces even if it's a single statement helps avoid bugs and clear out ambiguity.
Also, this code:
if (foo()) {
return true;
} else {
return false;
}
is not equivalent to
return foo();
because foo() could return something that isn't boolean. The correct short version would be:
return !!foo();
It's far less obvious what is going on here. That said, I'd probably go with:
return foo() ? true : false;