Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 95 Comments | 530 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

What the hell is up with people putting the value you're checking for first in a comparison expression? I freakin' hate that!!! Is there some sort of advantage? Arrrgh...

Maybe it's just me, but I think, “is <my variable> equal to <some value>?” That just makes sense to me. For instance, “Is your name Michael?” sounds so much better than, “Is Michael your name?” Granted, they both make grammatical sense. Maybe I'm just stuck in a latin-based society that reads (and thinks) left-to-right, but why the hell do people insist on writing expressions backwards (in my opinion, at least)?

If someone could point out a good reason (or ten) as to why this approach is preferred, I would appreciate it. I get stuck in my ways sometimes, but that's only because my way is the best way :-) ...well, until someone convinces me otherwise - and then, I just get a new way. So, in the end, my way is the best way, again. You gotta love my logic...well, I do, anyway.

posted on Tuesday, August 17, 2004 10:59 AM

Feedback

# re: null == annoyance 8/17/2004 11:18 AM thomas woelfer
Michael,

incase you have a typo in your code.... the c++ compiler will detect

if( 6 = x)

as an error, while

if( x = 6)

is perfectly legal. thus the tendency of a lot of developers to put the constants first.

WM_FYI
thomas woelfer

# re: null == annoyance 8/17/2004 12:21 PM Michael Flanakin
Interesting... I didn't know that. I've mainly seen this happen in C# code. I was annoyed with it while working on someone else's code and just recently noticed it in some Microsoft documentation that I read thru.

Thanks for the tidbit of information, tho.


# re: null == annoyance 8/17/2004 9:23 PM Thomas Williams
Michael, I'd hate to mess with you 'cause it sounds like you're seriously upset ;-)

I use VB.NET and I have started using the notation that Thomas Woelfer describes above in places - it makes calls to MessageBox.Show much more readable:

If DialogResult.No = MessageBox.Show(<all the arguments here>) Then...

Cheers,

# re: null == annoyance 8/17/2004 9:34 PM Michael Flanakin
I could see how that would be more readable, but what I would do in that situation is this... I'll even put it in VB! ;-)

Dim result As DialogResult
result = MessageBox.Show(...)
If result = DialogResult.No Then...

I know that this adds two lines and creates a variable that is only needed once, but I personally prefer this. While I don't like the order of the comparison, I'd accept it w/o too much complaining - as long as it didn't happen often.


# re: null == annoyance 3/11/2005 4:17 AM Drew Noakes
In Java I'd often write:

if ("".equals(myString))

...rather than the other way around. It avoided null pointer exceptions when myString was equal to null.

The == operator overload for strings in C# avoids the need for this, though it's still a useful trick on occasion.

Drew.

# re: null == annoyance 4/21/2005 2:20 PM dbnull
Thomas gives the correct reason that most programmers use the constant on the LHS of the comparison. You could argue that this is not required with Visual Studio as the compilers give a warning/error if you try:

if (blah=5)

Predicting that you meant:

if (blah==5)

The reason that C# programmers may still use this notation is through habit. In the C/C++ world it is considered good practice. It also suggests that the programmer is an experienced and knowledgeable C programmer.

# re: null == annoyance 5/11/2005 12:52 PM J. Daniel Smith
I agree that it is an annoyance. Most decent C++ compilers will catch the confusion these days -- especially if you compile with maximum warning level.

Personally, I go with "if (a == 3) ... "

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 2 and 5 and type the answer here: