How to use bool variable.

Started by
46 comments, last by Trienco 11 years, 3 months ago
Hey Cornstalks,
You might want to put a name to the practice of putting the variable last in a comparison. They're "Yoda conditions".
http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html

Ha, thanks for that. There are some pretty good ones there.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
Hey Cornstalks,
You might want to put a name to the practice of putting the variable last in a comparison. They're "Yoda conditions".
http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html

I think you mean, "Yoda conditions, they are."

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Quite frankly I see the use of if(whatever == true) as redundant code since it is almost common knowledge in coding that if(whatever) and if(whatever == true) are synonymous just as if(!whatever) and if(whatever == false) are synonymous. This really shouldn't matter in regards to true boolean logic as we are talking about coding standard, in real world application things like defining 1 to be true and 0 to be false with defines(like in C++) are just as stylistic as anything else.

if people really want to use the lengthier version that is up to them, but it is literally just that, longer.

The point of the code is for it to do what you -think- it does, not to do what a real world definition says it should, it is a loose standard not a limiting factor. Quite frankly if you're using truth tests in your code and set the value to 2 or 3 and are surprised it results in true then your logic for setting it to that number in the first place seems flawed.

The one place I would agree the syntax is silly is if(for some odd reason) someone did something like int whatever = 17, if(whatever) as opposed to some sort of greater than or equal equality test, but who codes like that?

in real world application things like defining 1 to be true and 0 to be false with defines(like in C++) are just as stylistic as anything else.

Uhm, true and false aren't defines in C++, because they aren't something that is just commonly tagged on like in C. That's actually making a very big difference, because in C a BOOL(EAN) is typically defined as int, while in C++ it is an actual type.

BOOLEAN b = 5;

if (b == TRUE)

is going to fail. Best example of that I found in the source of a certain game. Some genius decided to return "2" from a BOOLEAN function, because he needed a third result. Of course he didn't bother changing the function to int or some meaningful enum. Even less did that person bother to check all places where that function was already used. So other code that explicitly compared to "TRUE" was now broken.

So in C I would actually very much advice _against_ making explicit comparisons for the very reason that it's just defines (so to be safe, you would always have to either compare against FALSE or use some ridiculous trick like "if (!!variable == TRUE)").

Now, in C++

int x = 5;

if (x == true)

should work just fine (unless I'm getting my promotions wrong), because x will implicitly be turned into an actual bool of value "true". The compiler hopefully still slaps you with some kind of warning for doing this, though.

Still, just because explicit comparisons with "true" are "safe" in C++ doesn't make them any less pointless and redundant to me. The only argument I consider somewhat valid is that a "!" can easily be missed when browsing code, while "== false" is clunky enough to be noticed.

f@dzhttp://festini.device-zero.de

sad.png

read so many doscussions,I am even more confused.....

if(a) ? or if(a == true)? or all ok?

read so many doscussions,I am even more confused.....

if(a) ? or if(a == true)? or all ok?

Either one is ok, but if(a) is usually slightly preferred as a matter of personal preferences.

Now, in C++



int x = 5;

if (x == true)



should work just fine (unless I'm getting my promotions wrong), because x will implicitly be turned into an actual bool of value "true". The compiler hopefully still slaps you with some kind of warning for doing this, though.

You are getting your promotions wrong. `true' will be promoted to int with a value of 1 and the condition will be false.

Well, at least it's a good example why I never blindly rely on promotion rules and operator precedence. Also another (questionable) argument for not using "== true", at least if you're in the annoying habit of being sloppy with your variable types (the next time I see an unsigned short used in "if (v == -1)" I might have to scream).

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement