Why isn't it good to use long bool conditionals

Aug 31, 2019 at 8:09pm
closed account (1bRGhbRD)
Why do people say is better to code like so:

1
2
  bool X = true;
if(!X){//...} 

Rather than like so:
1
2
  bool X = true;
if(X == false){//...}. 
Aug 31, 2019 at 8:16pm
First one sounds like "if not blah then go"
second one sounds like "if blah equal false then go"

First just sounds more natural I guess

Edit: So like "if not cold then go" and "if cold equal false then go"
Last edited on Aug 31, 2019 at 8:18pm
Aug 31, 2019 at 8:27pm
closed account (1bRGhbRD)
Regarding performance. I so very vaguely recall learning it is bad practice due to how the compiler processes it.
Last edited on Aug 31, 2019 at 8:46pm
Aug 31, 2019 at 8:32pm
Oh
Aug 31, 2019 at 10:07pm
If you prefer if (X == false) then shouldn't you also prefer
if ((X == false) == true)?
https://en.wikipedia.org/wiki/Reductio_ad_absurdum
Aug 31, 2019 at 10:40pm
Regarding performance. I so very vaguely recall learning it is bad practice due to how the compiler processes it.
Even if there is some performance difference, that would be low-hanging fruit for any compiler optimizer. Leave such micro-optimization to the compiler author.

FWIW, I would be very surprised to find any difference at all between those cases.
Last edited on Aug 31, 2019 at 10:40pm
Sep 1, 2019 at 5:39am
closed account (1bRGhbRD)
It was a while back, I believe @jonnin said something about the processing. But that post is long drowned

Somebody used a system:

if(x == true){x==false}
And he advised him to use the other method
Last edited on Sep 1, 2019 at 5:44am
Sep 1, 2019 at 6:52am
It has nothing to do with parsing or code generation or anything like that.

It is because of the way C (and C++) handles expressions, and the slippery-slope for bad code that such constructs encourage.

In this case specifically, C has traditionally not had a boolean data type. Writing if (x == FALSE) has never been a problem -- it is when you then write things like if (x ==TRUE) when things go wrong.

Consequently, the conventional (and correct) wisdom is to let the compiler do its job properly performing implicit boolean conversions as a last step without your help.

That, and the truthiness of the statement isconsidered both obvious and cleaner to read without the extra verbiage.

if (x) ... if (!x) ...

Both are short, sweet, and to the point without ambiguity, just like C (and C++) programmers like it.

Hole this helps.
Sep 1, 2019 at 9:59am
closed account (1bRGhbRD)
You are thanked for your time, duthomas. Here is the reach of your generosity: https://discuss.cocos2d-x.org/t/jump-script-error/47555/5
Topic archived. No new replies allowed.