(5 == x) vs. (x == 5)
- Both are semantically identical.
- Both express exactly the same mathematical meaning.
- Both have the same performance (the compiler can swap them if
there's a difference in the assembly language, for some reason.) [snip]
- Both use the same number of characters, whitespace etc.
- Both require exactly the same amount of scanning to parse what
they are.
All the quoted statements are absolutely correct, and I don't believe
that anyone here has suggested otherwise.
But here's the statement that I temporarily snipped:
- If you preceive any intuitive difference its because you have a
defective brain; you should be banned from programming. Seriously.
This "intuition" cannot be explained by anything other than an
extremely weak grasp of elementary arithmetic.
That's not a statement about mathematics, programming, or C; it's a
statement about people and how they think. As such, it's not possible
to be as rigorous about it as about the other statements.
Nevertheless, I'm going to respond as if it were a black-and-white
issue, with the understanding that I'm merely stating my own opinion.
You're wrong.
If both forms of the expression are equally clear and intuitive to
you, that's great. But the fact remains that a lot of us find
(x == 5) easier to read than (5 == x). It's not because our brains
are defective, it's because the (x == 5) form happens to be what we're
used to, and perhaps because it's a closer match to how we'd express
it in English (or whatever our native language happens to be).
Suppose you're explaining the operation of a program, in spoken
English, and you want to tell your listener what the value of x is at
a certain point. Are you more likely to say "x is equal to 5", or
"5 is equal to x"? If you're telling me how old Fred is, are you more
likely to say "Fred's age is 30", or "30 is Fred's age" (or "30 years
old is Fred")? In each case, both forms are valid English, but the
first is more idiomatic, and the second is likely to make your
listener wonder why you're talking funny. (If the second form you
actually use in speech, Yoda you are, and my five pounds I claim
(obscure reference).)
Now I'll agree that any programmer should understand what (5 == x)
means, and shouldn't be confused by it for more than a moment. The
only reason people use that form, as far as I can tell, is to avoid
"=" vs. "==" errors. I've programmed in languages where "=" is used
for comparison, ":=" is used for assignments, and assignments cannot
occur in expressions; in such languages, I've never seen the reversed
form (e.g., "if 5 = X then ...").
I can see both sides of the issue. I prefer the (x == 5) form, but I
can deal with (5 == x), and avoiding "=" vs. "==" errors is a valid
reason to use it. But suggesting that anyone who disagrees with you
has a defective brain is not a good way to win arguments.