A
Alex
I know this is a religious issue, and I hope this thread doesn't degenerate,
but I am curious about the pros and cons of swapping the operands of ==.
The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.
But the advantage above only applies if one of the operands is a constant.
This is certainly not always the case, and I suspect it would often be
harder to diagnose accidental assignment to another variable than to a
constant. Some compilers are helpful enough to issue a warning in any case.
Finally, as a native English speaker I find (5 == x) less readable than (x
== 5) (and I imagine the same goes for many if not most other languages).
If you use (5 == x) style, do you have to track down bugs in your code which
are due to accidental assignment where both operands are variables? And do
you also put the constant (if there is one) on the LHS in a relational
expression?
Alex
but I am curious about the pros and cons of swapping the operands of ==.
The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.
But the advantage above only applies if one of the operands is a constant.
This is certainly not always the case, and I suspect it would often be
harder to diagnose accidental assignment to another variable than to a
constant. Some compilers are helpful enough to issue a warning in any case.
Finally, as a native English speaker I find (5 == x) less readable than (x
== 5) (and I imagine the same goes for many if not most other languages).
If you use (5 == x) style, do you have to track down bugs in your code which
are due to accidental assignment where both operands are variables? And do
you also put the constant (if there is one) on the LHS in a relational
expression?
Alex