It isn't, in general. For compiled languages the expression will be likely reduced to the same machine code, and in interpreted languages the explicit test will help reduce the chance of unwanted code insertion.
Consider the following JavaScript function, to, say, tally votes on a hypothetical ballot measure.
function recordVote(YesOrNo) {
if(YesOrNo == true) {
voteYes();
} elseif (YesOrNo == false) {
voteNo();
}
}
If you call recordVote(true)
or recordVote(false)
, the interior method fires as expected. And, if you improperly call recordVote(42)
, recordVote(null)
, or recordVote("false")
, you get no action -- which is desired for a voting app. (especially since the standard test for truthyness, of if(YesOrNo)
, would cause that last one to record a vote incorrectly.
Of course, note that the small benefits you get from the above are inferior to consistency in your code. If the library's already full of them, don't remove them -- but don't add them if it isn't. (Unless, of course, you're refactoring everything anyway and have some arbitrary style rule to force your source code to be as small as possible...)
operator==
yields a boolean value itself, your followup question should be: "Don't you think writing(bBool == true) == true
makes the code even more straightforward to understand? Why don't you use that style?".int
or not...(anInt == 0) == true
? I agree that for numeric results you should have an explicit test. What I said is that if you require explicit tests for operations that result in a boolean value, then you should apply that exact same logic to the result ofoperator==
.!anInt
with unexpected results...