if (i == 2) or if (2 == i) ?
- by Maroloccio
I usually use
if (i == 2)
in preference to
if (2 == i)
On occasion, I would switch things around when writing xUnit-type tests from scratch so as to follow the assertion convention of "expected" preceding "actual".
When adding to existing unit tests, I always follow the style I find.
No matter what, I always try to keep things consistent.
Today I checked out some code with a lot of "if (2 == i)" and started wondering:
which style is more "popular" nowadays?
is popularity language-dependent?
The latter probably because I am aware of why the "if (2 == i)" became common in the first place (C heritage) and because I see some languages go as far as disallowing assignments within conditions (e.g. Python).
I thought about downloading some sources:
apt-get source linux-source eclipse openoffice.org
expanding them and performing a quick grep:
grep --color --include=*.java --include=*.c -ERI \
"if[[:space:]]*\([[:space:]]*[[:digit:]]+[[:space:]]==" .
or creating a quick "poll":
http://goo.gl/mod/ciMF
after a bit of searching and asking around, I am still not sure.
So I am asking you:
which way to go?