views:

76

answers:

5

I've seen this around, but never heard a clear explanation of why... This is really for any language, not just C# or VB.NET or Perl or whatever.

When comparing two items, sometimes the "check" value is put on the left side instead of the right. Logically to me, you list your variable first and then the value to which you're comparing. But I've seen the reverse, where the "constant" is listed first.

What (if any) gain is there to this method?

So instead of:

if (myValue > 0)

I've seen:

if (0 < myValue)

or

if (Object.GimmeAnotherObject() != null)

is replaced with:

if (null != Object.GimmeAnotherObject())

Any ideas on this?

TIA! Kevin

+2  A: 

I don't think a simple rule like but the constant first or but the constant last is not a very smart choice. I believe the check should express it semantics. For example I prefer to use both versions in a range check.

if ((low <= value) && (value <= high))
{
    DoStuff(value);
}

But I agree on the examples you mentioned - I would but the constant last and can see no obviouse reason for doing it the other way.

if (object != null)
{
    DoStuff(object);
}
Daniel Brückner
+5  A: 

Some developers put the constant on the left like so

if(0 = myValue)

This is because you will get an error from the compiler, since you can't assign 0 a value. Instead, you will have to change it to

if(0 == myValue)

This prevents lots of painful debugging down the road, since typing

if(myValue = 0)

is perfectly legal, but most likely you meant

if(myValue == 0)

The first choice is not what you want. It will subtly change your program and cause all sorts of headaches. Hope that clarifies!

samoz
keep in mind that in many newer languages if(myValue = 0) isn't valid code as myValue = 0 evaluates to a number and if( ) doesn't work with a number as the condition
David Johnstone
@David Welcome to the world of pseudocode :)
samoz
+2  A: 

In C++ both these are valid and compile

if(x == 1)

and

if(x=1)

but if you write it like this

if(1==x)

and

if(1=x)

then the assignment to 1 is caught and the code won't compile.

It's considered "safer" to put the const variable on the left hand side.

Once you get into the habit of putting the const variable on the left for assignment it tends to become your default mode of operation, that's why you see it showing up in equality checks as well

Glen
+1  A: 

Its a coding practice to catch typos like '!=' typed as '=' for example.

If you have a CONSTANT on the left all assignment operators will be caught by the compiler since you cannot assign to a constant.

Many languages (specifically C) allow a lot of flexibility in writing code. While, the constant on the left seems unusual to you, you can also program assignments and conditionals together as,

if (var1 = (var2 & var3)) { /* do something */ }

This code will get the boolean result into var1 and also /* do something */ if the result is true.

A related coding practice is to avoid writing code where conditional expressions have assignments within them; though the programming language allows such things. You do not come across such code a lot since assignments within conditionals is unusual so typical code does not have such things.

There is a good C language coding practices article at the IBM DeveloperWorks site that is probably still relevant for people writing in that language.

nik
You have to read the conditional here carefully to notice the amount of things jammed into it. For, example, on the first reading did you observe that var1 will actually get a bit-wise AND result. And that such a results is TRUE when it is non zero.
nik
Writing readable and debug-able code implies that such trickery is avoided. Let your stunts focus on the efficiency of your code, not in its 'calligraphy' :-)
nik
+2  A: 

For .Net, it's irrelevant, as the compiler won't let you make an assignment in a condition like:

if(x=1)

because (as everyone else said) it's bad practice (because it's easy to miss).

Once you don't have to worry about that, it's slightly more readable to put the variable first and the value second, but that's the only difference - both sides need to be evaluated.

Robin Bennett