[I thought this would have been asked already, but I can't find an answer].
When writing an if
statement, should the thing I'm comparing against come first or second? (I am specifically curious about both C / C++
and Java
).
Does it matter, or is it just stylistic? (Convention seems to be "variable first, value second").
Consider the following code:
#include <iostream>
int main() {
int x = 5;
if ( 5 == x ) {
std::cout << "X == 5, true." << std::endl;
}
if ( x == 5 ) {
std::cout << "5 == X, true." << std::endl;
}
return 0;
}
which outputs:
X == 5, true.
5 == X, true.
so there doesn't appear to be any difference. Are there subtleties I'm missing?