Style question: (5 == x) or (x == 5)

R

Richard Bos

Mark A. Odell said:
Why? Do you find:

if (foo == bar)

more readable than

if (bar == foo)

No, because foo and bar carry no meaning. However, I do find

if (the_limit == current_value)

less clear than

if (current_value == the_limit)

Not systactically, mind you - semantically.

Richard
 
R

Ross Kendall Axe

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

August Derleth wrote:
| On Thu, 06 May 2004 19:02:29 +0100, Brian Gough wrote:
|
|
|>
|>
|>>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).
|>
|>GCC warns about assignments inside conditionals (with -Wall), so it's
|>less of an issue when using GCC.
|
|
| To be fair, most decent compilers should diagnose the same problem. And if
| yours doesn't, lint or its close relatives most certainly will.
|
| The fact that lint and its close relatives also diagnose things that
| aren't at all dangerous is incidental.
|

Using if((x=5)) instread of if(x=5) supresses the warning in gcc. I was
wondering if that was also true of lint/other compilers.

Ross
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAm6cC9bR4xmappRARAnryAJ9OvPoZZe1utxTP6bxkCfD6hZVjDgCdEqoq
9HWVvJT7nucgm6HFVzrm0ZY=
=bgK9
-----END PGP SIGNATURE-----
 
B

Ben Pfaff

Ross Kendall Axe said:
Using if((x=5)) instread of if(x=5) supresses the warning in gcc. I was
wondering if that was also true of lint/other compilers.

Other compilers I've used only suppress the warning if you write
if ((x = 5) != 0). As far as I know suppression via doubled
parentheses is a GCC quirk.
 
A

Alex Fraser

CBFalconer said:
if (5 == foo(myriad, imcomprehensible, arguments, of, stuff)) {
/* action */
}

where the critical test is shown right next to the if, i.e. foo
returns a specific value.

For readability, I try to avoid long if-expressions, especially if they are
so long they need a line break or two. Thus I would probably write the above
as:

int result;

result = foo(myriad, imcomprehensible, arguments, of, stuff);
if (result == 5) {
/* action */
}
The ONLY real argument against it is that it appears strange to
some.

Most, I think. But likewise, the only real argument for it that I can see is
that it avoids possible errors in strictly limited circumstances with some
compilers.
Well, the whole C language appears exceedingly strange to many.

True; but all the more reason to make it as little strange as possible.

Alex
 
E

E. Robert Tisdale

Alex said:
For readability, I try to avoid long if-expressions,
especially if they are so long they need a line break or two.
Thus I would probably write the above as:

// int result; // result is uninitialized
const
int result = foo(myriad, imcomprehensible, arguments, of, stuff);
if (result == 5) {
/* action */
}
 
C

Christian Bau

Another example of Trollsdale modifying quoted text.

Tisdale, you are a liar.

By the way, your programming style is quite horrid.
 
M

Mabden

Ben Pfaff said:
Other compilers I've used only suppress the warning if you write
if ((x = 5) != 0). As far as I know suppression via doubled
parentheses is a GCC quirk.

Microsoft compilers (MSVC and VS6) only warn about "if (x=5)" if you set the
warning level up to 4 (the max). Level 4 also warns about non-ANSI code and
is generally a PITA. I usually use level 3, so I would not have seen a
warning about this code. However "if (5=x)" will not compile even with
warnings turned off.

I guess Microsoft embraces the "Trust the Programmer" motto of C. }:-0

I am only starting to use this method myself, when I write in C, and I'm not
totally comfortable with seeing it, but I agree with the poster who said it
was worth getting used to if it saves you from one maddening debug session.
Especially when it's your code and you KNOW you coded it right! ;-)
 
C

CBFalconer

Christian said:
Another example of Trollsdale modifying quoted text.

Tisdale, you are a liar.

Let's be accurate, much as it hurts to concede anything to
Trollsdale. He didn't modify this quote, he injected a separate
line (as you can see from the quote markers) which is immediately
identifiable as to origin and thus acceptable. I haven't noticed
any of his sneaky revisions for a while - is it possible he has
reformed?
 
J

Joona I Palaste

Let's be accurate, much as it hurts to concede anything to
Trollsdale. He didn't modify this quote, he injected a separate
line (as you can see from the quote markers) which is immediately
identifiable as to origin and thus acceptable. I haven't noticed
any of his sneaky revisions for a while - is it possible he has
reformed?

Yes he did modify the quote. Here is Alex's original code:
------------------------------------------------------------------
int result;

result = foo(myriad, imcomprehensible, arguments, of, stuff);
if (result == 5) {
/* action */
}
 
M

Malcolm

CBFalconer said:
if (5 == foo(myriad, imcomprehensible, arguments, of, stuff)) {
/* action */
}

where the critical test is shown right next to the if, i.e. foo
returns a specific value. The 5 and the foo are strongly
associated. I would have stayed out of this except that I haven't
seen the above argument elsewhere.
This is quite a good argument.
However you could write

symbolsatdoor = foo(myriad, incomprehensible, arguments, of, stuff) ;
if(symbolsatdoor == 5)

This has the advantage of helping to document what foo() does.

Also consider that usually we wouldn't have a constant 5 but a symbol eg
#define SYMBOLSATDOOR 5

Now it becomes much more important to write
if(val == SYMBOLSATDOOR)

to make clear that val is the variable whilst SYMBOLSATDOOR is the constant
it is being compared to.
 
M

Malcolm

E. Robert Tisdale said:
I have gotten used to writing (5 == x) instead of (x == 5)
and now (x == 5) looks strange to me -- in fact, I find it alarming!
That strategy is fine as long as C and its relations are the only
programming languages that you use.
 
P

Paul Hsieh

(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.)
- 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.
- Both use the same number of characters, whitespace etc.
- Both require exactly the same amount of scanning to parse what
they are.

In short, when rendered properly, they are identical. They are just
the same.

Of course (5 == x) has the advantage that if you accidentally drop one
of the ='s the compiler will catch the problem immediately, while (x
== 5) will simply change the semantics (unless you have set your
warning level to maximum, and your compiler detects these.)

That tips the balance. (5 == x) is simply superior, because of this
single programming-centric distinguishing attribute.

I've done mathematics all my life -- I don't understand the argument
that when you learned mathematics would make you think that there is
any difference between the two. In fact it should be quite the
opposite -- any reasonable working knowledge of elementary mathematics
should pretty much assure that you see no intuitive difference.
 
C

CBFalconer

Joona said:
Yes he did modify the quote. Here is Alex's original code:
------------------------------------------------------------------
int result;

result = foo(myriad, imcomprehensible, arguments, of, stuff);
if (result == 5) {
/* action */
}
------------------------------------------------------------------

Not only has Trollsdale commented out the "int result;" line, he
has added an "int" keyword in front of the "result = ..." line,
making it a declaration statement instead of a normal statement.

I have been fooled again by Trollsdale. Once again, the
Trollsdale is quicker than the eye. It must be cackling over the
number of quick ones it has slipped past us.
 
M

Malcolm

Paul Hsieh said:
(5 == x) vs. (x == 5)

- 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.
Or use of natural language.

consider a function that use x for the vertical axis and y for the
horizontal. Of course the compiler couldn't care, and mathematically the
symbols are the same. However because of the convention that x is the
independent and the horizontal axis, this code will be difficult to read.

Or consider an expression with four levels of parentheses. Again, it is easy
to write an automatic parser that can handle arbitrary depth of nesting.
Humans are limited to about three levels.

It is not that people who write x == 5 are too stupid to see that 5 == x is
equivalent. They realise that if the brain has to do extra work to convert
mathematical order into natural language order then it will be less likely
to spot bugs in other parts of the expression.
 
C

Crazy Coder

Alex said:
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

one might look at the reverse issue too, in case another programmer
has to maintain the code with many

(5 == x) type of comparisions

it would make code review/maintaince a lot more difficult cause one
would have to do a mental switch of reading the equality comparisions
from "right to left".

i.e. i'm looking at "if 5 equal to x" but i need to interpret is as
"if x equal to 5).

yb.
 
C

Christian Bau

================

This line is different from the original post.

Let's be accurate, much as it hurts to concede anything to
Trollsdale. He didn't modify this quote, he injected a separate
line (as you can see from the quote markers) which is immediately
identifiable as to origin and thus acceptable. I haven't noticed
any of his sneaky revisions for a while - is it possible he has
reformed?

Yes, he modified the quote.

The original was:

int result;
result = foo (...);

Which cannot be changed to

const int result;
result = foo (...);

So he changed this to

const int result = foo (...);
 
K

Keith Thompson

(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.
 
C

CBFalconer

Keith said:
.... snip ...

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.

However it can be effective in launching flame wars.

Consider "if (5 is the value returned by x) then ...".
 
A

Alan Balmer

Microsoft compilers (MSVC and VS6) only warn about "if (x=5)" if you set the
warning level up to 4 (the max).

Which is where it should always be.
Level 4 also warns about non-ANSI code and
is generally a PITA. I usually use level 3, so I would not have seen a
warning about this code.

I'm glad I don't have to use your code.
However "if (5=x)" will not compile even with
warnings turned off.

Of course not - it's an error.
I guess Microsoft embraces the "Trust the Programmer" motto of C. }:-0

I am only starting to use this method myself, when I write in C, and I'm not
totally comfortable with seeing it, but I agree with the poster who said it
was worth getting used to if it saves you from one maddening debug session.

You would prevent even more debugging sessions by turning the warning
level up to the max.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top