I was always taught to put the variable you are checking to the Left side example
if (myvariable == null)
whilst technically not incorrect to do
if (null == myvariable)
is there any programmatic reason you would do this?
I was always taught to put the variable you are checking to the Left side example
if (myvariable == null)
whilst technically not incorrect to do
if (null == myvariable)
is there any programmatic reason you would do this?
suppose you forget == and write =. In this case x=5 will be compiled but 5=x not.
My first boss used to insist on putting null
on the left hand side. His argument was that some day you will leave out an =
and save yourself possibly lots of time debugging, since you can't assign to null so it will throw an error.
Consider the following c#
var y = false;
var x = y = true;
// y = true, x = true.
and
var y = false;
var x = y == true;
// x = false, y = false
These both end up with different y and x values, but it would be easy to type one when you meant the other. However, if you have the habit of putting your constant values on the left hand side of the operator, then you will get compiler errors instead.
var x = true = y; // won't compile
var x = true == y; // will compile
Also, if you write in vb and c# a lot, vb only has 1 equal for comparison, so it definitely can be a good idea if you're coding in both languages, as you may copy and paste some code and suddenly what was a comparison is now assigning.