Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "if ([bool] == true)" require one more step than "if ([bool])"?

This is a purely pedantic question, to sate my own curiosity.

I tend to go with the latter option in the question (so: if (boolCheck) { ... }), while a coworker always writes the former (if (boolCheck == true) { ... }). I always kind of teased him about it, and he always explained it as an old habit from when he was first starting programming.

But it just occurred to me today that actually writing out the whole == true part may in fact require an additional step for processing, since any expression with a == operator gets evaluated to a Boolean value. Is this true?

In other words, as I understand it, the option without the == true line could be loosely described as follows:

  1. Check X

While the option with the == true line would be more like:

  1. Let Y be true if X is true, otherwise false
  2. Check Y

Am I correct? Or perhaps any normal compiler/interpreter will do away with this difference? Or am I overlooking something, and there's really no difference at all?

Obviously, there will be no difference in terms of actual observed performance. Like I said, I'm just curious.

EDIT: Thanks to everyone who actually posted compiled results to illustrate whether the steps were different between the two approaches. (It seems, most of the time, they were, albeit only slightly.)

I just want to reiterate that I was not asking about what is the "right" approach. I understand that many people favor one over the other. I also understand that, logically, the two are identical. I was just curious if the actual operations being performed by the CPU are exactly the same for both methods; as it turns out, much of the time (obviously it depends on language, compiler, etc.), they are not.

like image 998
Dan Tao Avatar asked Jul 01 '09 16:07

Dan Tao


People also ask

Is 1 true or false in bool?

Boolean Variables and Data Type ( or lack thereof in C ) C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

Is bool always 0 or 1?

Boolean values and operations C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.

What does if bool mean?

It simply inverts the value of the bool expression. True becomes False and False becomes True . if block will run only if expression inside the parentheses evaulates to True .

Is 0 true or false in bool?

Also, a numeric value of zero (integer or fractional), the null value ( None ), the empty string, and empty containers (lists, sets, etc.) are considered Boolean false; all other values are considered Boolean true by default.


2 Answers

I think the comparison with true shows a lack of understanding by your partner. Bools should be named things to avoid this (such as isAvailable: if (isAvailable) {...}).

like image 99
rlbond Avatar answered Oct 19 '22 22:10

rlbond


I would expect the difference to be optimised away by any half-decent compiler.

(I just checked with C# and the compiled code is exactly the same for both syntaxes.)

like image 35
LukeH Avatar answered Oct 20 '22 00:10

LukeH