Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Style question: (5 == x) or (x == 5)

Reply
Thread Tools

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

 
 
Alex
Guest
Posts: n/a
 
      05-06-2004
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


 
Reply With Quote
 
 
 
 
Darrell Grainger
Guest
Posts: n/a
 
      05-06-2004
On Thu, 6 May 2004, Alex wrote:

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

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      05-06-2004
"Alex" <(E-Mail Removed)> wrote in news:(E-Mail Removed):

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

--
- Mark ->
--
 
Reply With Quote
 
Frane Roje
Guest
Posts: n/a
 
      05-06-2004

"Darrell Grainger" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      05-06-2004
"Alex" <(E-Mail Removed)> writes:

> 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


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-06-2004
Darrell Grainger wrote:
>
> On Thu, 6 May 2004, Alex wrote:
> > [...]
> > 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 )' ...

--
(E-Mail Removed)
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-06-2004
Alex wrote:
> ...
> 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.

--
Best regards,
Andrey Tarasevich


 
Reply With Quote
 
Brian Gough
Guest
Posts: n/a
 
      05-06-2004
"Alex" <(E-Mail Removed)> writes:

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

--
Brian Gough

Network Theory Ltd,
Publishing Free Software Manuals --- http://www.network-theory.co.uk/
 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      05-06-2004
"Frane Roje" <frane.roje(*dele*te)@st.htnet.hr> wrote in
news:c7dsim$jlr$(E-Mail Removed):

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


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

--
- Mark ->
--
 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      05-06-2004
"Mark A. Odell" <(E-Mail Removed)> wrote in message
news:Xns94E188816C99BCopyrightMarkOdell@130.133.1. 4...
> "Alex" <(E-Mail Removed)> wrote in news:(E-Mail Removed):

[snip]
> > 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.


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments