Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


How to use bool variable.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
47 replies to this topic

#41 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 25 December 2012 - 09:01 PM

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.


[ 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 ]

Sponsor:

#42 Khatharr   Crossbones+   -  Reputation: 3040

Like
1Likes
Like

Posted 26 December 2012 - 01:37 PM

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.

#43 Satharis   Members   -  Reputation: 1267

Like
0Likes
Like

Posted 26 December 2012 - 02:57 PM

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?


Edited by Satharis, 26 December 2012 - 02:58 PM.


#44 Trienco   Crossbones+   -  Reputation: 2224

Like
1Likes
Like

Posted 27 December 2012 - 12:13 AM

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

#45 xiajia   Members   -  Reputation: 163

Like
0Likes
Like

Posted 27 December 2012 - 06:45 AM

sad.png

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

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



#46 Servant of the Lord   Crossbones+   -  Reputation: 21159

Like
0Likes
Like

Posted 27 December 2012 - 08:19 AM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#47 Álvaro   Crossbones+   -  Reputation: 13933

Like
2Likes
Like

Posted 27 December 2012 - 09:13 AM

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.

#48 Trienco   Crossbones+   -  Reputation: 2224

Like
0Likes
Like

Posted 27 December 2012 - 11:22 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS