The advantage is compiler will tell you that error right away. For example, a = 1
will compile but will produce an error at run time whereas 1 = a
will produce an error at compile time since 1
is not a valid lvalue.
Example: this will produce an error right away:
int a = 0;
if(1 = a) {
print("test");
}
Whereas this will compile (warnings may be produced depending on the compiler) but it will cause issues at run time.
int a = 0;
if(a = 1) {
print("test");
}
The main idea here is to make certain that you are using ==
in a condition instead of =
.
That being said every modern compiler (ie. Eclipse) will treat the above as an error now. So it's not as big of a deal as it used to back in the notepad and vi days (in my opinion). I personally prefer the a == 1
since that seems more readable to me.