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

A

Alex

I know this is a religious issue, and I hope this thread doesn't degenerate,
but I am curious about the pros and cons of swapping the operands of ==.

The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.

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).

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
 
D

Darrell Grainger

I know this is a religious issue, and I hope this thread doesn't degenerate,
but I am curious about the pros and cons of swapping the operands of ==.

The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.

This is the only time I have heard of (5 == x) being used. If there is
another advantage, I do not remember heard of it.
But the advantage above only applies if one of the operands is a constant.

Quite true.
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.

Since it a common mistake I would consider it a necessary feature for a
compile to warn about this.
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).

I'm not sure if it is restricted to native English speakers. I picked up
the (x == 5) format from mathematics. I've worked with people who I have
great difficulty talking with because their English is poor and my grasp
of their language is just as bad but when we talk about mathematics there
is usually little misunderstanding. For basic mathematics I think
(variable = constant) is a standard format, regardless of language.

So if you can do (var = const) but still remain alert enough to use (const
== var) then you probably would not make the mistake of using assignment
when you meant comparison.
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?

I was doing math for years before I learned to program in C. It just
throws me to write (5 == x) so I still use (x == 5) and trust the compiler
to warn me if I should slip and use (x = 5). If the compiler doesn't do it
I'd use lint.
 
M

Mark A. Odell

Alex said:
I know this is a religious issue, and I hope this thread doesn't
degenerate, but I am curious about the pros and cons of swapping the
operands of ==.

The only pro of putting the constant on the LHS that I know of is that
it protects against an accidental assignment where a test for equality
was intended.

It just a safety that comes for free, you don't have to use it. You could
just as well cast the var. to a const type too, e.g.

int foo = getSomeValue();

if ((const int) foo == 5)
in any case. Finally, as a native English speaker I find (5 == x) less
readable than (x >== 5)

Why? Do you find:

if (foo == bar)

more readable than

if (bar == foo)

?

if (5 == value) should be no different.
 
F

Frane Roje

Darrell Grainger said:
On Thu, 6 May 2004, Alex wrote:
I'm not sure if it is restricted to native English speakers. I picked up
the (x == 5) format from mathematics. I've worked with people who I have
great difficulty talking with because their English is poor and my grasp
of their language is just as bad but when we talk about mathematics there
is usually little misunderstanding. For basic mathematics I think
(variable = constant) is a standard format, regardless of language.

With this I would agree, and I'm not a native Endglish speaker plus
when you solve an equotation you always write x = sometnihg even
if the result comes at the end sometnig = x.


--
Frane Roje

Have a nice day

Remove (*dele*te) from email to reply
 
M

Martin Dickopp

Alex said:
I know this is a religious issue, and I hope this thread doesn't degenerate,
but I am curious about the pros and cons of swapping the operands of ==.

The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.

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).

I personally prefer `x == 5', but as you correctly observe, this is a
question of religion^H^H^H^H^H^H^H^Hstyle.

The more important thing, IMHO, is to pick one style and then use it
consistently.

Martin
 
E

Eric Sosman

Darrell said:
[...]
The only pro of putting the constant on the LHS that I know of is that it
protects against an accidental assignment where a test for equality was
intended.

This is the only time I have heard of (5 == x) being used. If there is
another advantage, I do not remember heard of it.
But the advantage above only applies if one of the operands is a constant.

Quite true. [...]

Well, there's always `if ( (0,x) == y )' ...
 
A

Andrey Tarasevich

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?
...

In my opinion this question is closely related to another one: do you
actually use assignment in conditional expressions at all? I don't. I
always do assignment separately and I always write comparisons of the
above kind as 'x == 5' because it looks more natural to me. I've never
had any problems with "accidental assignment" in my code.

On the other hand, I can imagine that people who do actively use
assignment in conditional expressions might run into this "accidental
assignment" problem more often and for them sticking to '5 == x' style
might make much more sense.
 
B

Brian Gough

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

Mark A. Odell

With this I would agree, and I'm not a native Endglish speaker plus
when you solve an equotation you always write x = sometnihg even
if the result comes at the end sometnig = x.

Yes but that's assignment (=) not an equality check (==).
 
A

Alex Fraser

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

if (foo == bar)

more readable than

if (bar == foo)

?

if (5 == value) should be no different.

Your choice of variable names hides the issue, the reduced readability is a
function of the context. Usually, when two variables are compared I would
consider there to be a clear "natural" order (this applies to both the
equality and relational operators).

That said, I'm struggling to think how exactly this natural order can be
defined. All I can think of right now are situations where one of the
variables is conceptually a constant at the time of the comparison; to me,
that variable naturally belongs on the RHS just as a constant would.

Alex
 
E

E. Robert Tisdale

Alex wrote:

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).

Nonsense!

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!

Human beings can get used to just about anything.
What is important here is consistency.
Pick a style and stick to it. Try to be as anal as possible.
This will help you to recognize mistakes in your code.
Don't worry about other people reading your code.
They will quickly adapt to your style
or the will use a code reformatter to convert it to a style
which they find more comfortable to read.
 
M

Mark McIntyre

With this I would agree, and I'm not a native Endglish speaker plus
when you solve an equotation you always write x = sometnihg even
if the result comes at the end sometnig = x.

thats not entirely true.
5 <=x <= 10
 
T

Thomas stegen

Mark said:
Yes but that's assignment (=) not an equality check (==).

I mathematics it is a statements saying the two sides are
equal, there is no such thing as assignment when solving equations.
 
G

Guillaume

if (5 == value) should be no different.

It probably comes from a mathematical background.

I think we are an awful lot of people who just find 'x = 5' more
"appealing" than '5 = x', and likewise, in C, 'x == 5' is more
readable than '5 == x'. It's just so, whether you like it or not.

The point of "erroneous assignment when an equality test was meant"
is kind of moot in my opinion. To begin with, most decent compilers
will give you a warning if you give it 'if (x = 5)'. Besides, not to
sound stuck-up, but I consider this kind of error a beginner's mistake.
Not one of a professional programmer.
 
M

Mark McIntyre

will give you a warning if you give it 'if (x = 5)'. Besides, not to
sound stuck-up, but I consider this kind of error a beginner's mistake.
Not one of a professional programmer.

Hmm. Anyone who says "Oh, I never make elementary mistakes" is a liar.
Everyone, without exception, does it from time to time.
 
C

CBFalconer

Darrell said:
.... snip ...

I was doing math for years before I learned to program in C. It
just throws me to write (5 == x) so I still use (x == 5) and
trust the compiler to warn me if I should slip and use (x = 5).
If the compiler doesn't do it I'd use lint.

It certainly is a religious issue, and I am on the (5 == x) side.
The first time it saves you a long bug hunt it has paid for
itself. I also use it for !=, <=, >=, etc. because such test do
get revised later, and it keeps me in the habit. Also consider:

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.

The ONLY real argument against it is that it appears strange to
some. Well, the whole C language appears exceedingly strange to
many.
 
W

William Ahern

It certainly is a religious issue, and I am on the (5 == x) side.
The first time it saves you a long bug hunt it has paid for
itself. I also use it for !=, <=, >=, etc. because such test do
get revised later, and it keeps me in the habit. Also consider:
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.

I was going to give the same example but bugged out at the last second ;)

While I typically use 'x == 5' I almost always use '5 == foo(...)'. The
latter requires less text scanning (by your eyes) because you're almost
always interested in the function and its return value; they are, as CB
said, the most strongly associated. Now-a-days, 'foo(...) == 5' appears to
me as needless obfuscation.

- Bill
 
C

CBFalconer

Guillaume said:
.... snip ...

The point of "erroneous assignment when an equality test was
meant" is kind of moot in my opinion. To begin with, most decent
compilers will give you a warning if you give it 'if (x = 5)'.
Besides, not to sound stuck-up, but I consider this kind of error
a beginner's mistake. Not one of a professional programmer.

I have been making neophyte mistakes in many languages for a very
long time. I am not too proud to take an advantage when
available. In addition, I have to battle this pesky keyboard,
which sometimes seems to have ideas of its own.
 
A

August Derleth

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.
 
I

Irrwahn Grausewitz

Mark McIntyre said:
thats not entirely true.
5 <=x <= 10

.... which translates nicely into:

( 5 <= x ) && ( x <= 10 )

Alas, I'm not sure if this might be used to form an argument pro or
con the "write it in C like you'd do in math expressions" style...

Regards
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top