if (23 == x) or if (x == 23)
-
Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!
-
Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!
It is to prevent typo errors,
if (x=23)
will compile and do undesired things, assign x the value of 23 and always be trueif (23=x)
will not compile its good practice to put the constant on the left and variable on the right -
Looking though some people's code I often see if statements of the form if (23 == x) rather than if (x == 23) Is there any reason for doing this? I'm sure there is !!!
I have thought myself about it and could only think of one reason - to avoid mistakes with 'if (x = 23)'. Most compilers today warns you about such syntax, but in past that could be a common hard-to-find mistype. By having accustomed to write constant first, you ensure compiler would not handle this: 'if (23 = x)' Igor Green http://www.grigsoft.com/ Compare It! + Synchronize It! - files and folders comparison never was easier!