Magical if-statement

Recommended Posts

ysg    192

if (foo == foo || bar == bar) {
...
}
else {
...
}

This is an old one.  My software lead did this, I showed it to her and she said: Umm... I... yeah... I wasn't thinking :) .

We both laughed and I rewrote it all :) .

Share on other sites
ysg    192

If it's a floating point number with a NaN value then (foo != foo) and the code could go into the else block.

I'm 90% certain that it was an into of some sort.  It was pretty funny and

we both were like, yeah, this will not be mentioned in polite company

ever again :) .

Share on other sites
Buster2000    4310

Couldn't foo and bar also be declared volotile?

Share on other sites

when x is a floating point number, if (x!=x) permits to check if x is NaN. This is terribly ugly .... but that's it

Edited by Tournicoti

Share on other sites
ysg    192

Couldn't foo and bar also be declared volotile?

I don't think I've ever used volatile in my entire coding career.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account