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

Discussion in 'C Programming' started by Alex, May 6, 2004.

  1. Alex

    Alex Guest

    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
     
    Alex, May 6, 2004
    #1
    1. Advertising

  2. 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
     
    Darrell Grainger, May 6, 2004
    #2
    1. Advertising

  3. "Alex" <> wrote in news::

    > 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 ->
    --
     
    Mark A. Odell, May 6, 2004
    #3
  4. Alex

    Frane Roje Guest

    "Darrell Grainger" <> wrote in message
    news:p...
    > 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
     
    Frane Roje, May 6, 2004
    #4
  5. "Alex" <> 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. \_/
     
    Martin Dickopp, May 6, 2004
    #5
  6. Alex

    Eric Sosman Guest

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

    --
     
    Eric Sosman, May 6, 2004
    #6
  7. 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
     
    Andrey Tarasevich, May 6, 2004
    #7
  8. Alex

    Brian Gough Guest

    "Alex" <> 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/
     
    Brian Gough, May 6, 2004
    #8
  9. "Frane Roje" <frane.roje(*dele*te)@st.htnet.hr> wrote in
    news:c7dsim$jlr$:

    >> 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 ->
    --
     
    Mark A. Odell, May 6, 2004
    #9
  10. Alex

    Alex Fraser Guest

    "Mark A. Odell" <> wrote in message
    news:Xns94E188816C99BCopyrightMarkOdell@130.133.1.4...
    > "Alex" <> wrote in news::

    [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
     
    Alex Fraser, May 6, 2004
    #10
  11. 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.
     
    E. Robert Tisdale, May 6, 2004
    #11
  12. On Thu, 6 May 2004 19:27:54 +0200, in comp.lang.c , "Frane Roje"
    <frane.roje(*dele*te)@st.htnet.hr> wrote:

    >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


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, May 6, 2004
    #12
  13. Mark A. Odell wrote:

    > "Frane Roje" <frane.roje(*dele*te)@st.htnet.hr> wrote in
    > news:c7dsim$jlr$:
    >>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 (==).
    >


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

    --
    Thomas.
     
    Thomas stegen, May 6, 2004
    #13
  14. Alex

    Guillaume Guest

    > 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.
     
    Guillaume, May 6, 2004
    #14
  15. On Thu, 06 May 2004 23:19:36 +0200, in comp.lang.c , Guillaume
    <> wrote:

    >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.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, May 6, 2004
    #15
  16. Alex

    CBFalconer Guest

    Darrell Grainger wrote:
    >

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

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, May 7, 2004
    #16
  17. CBFalconer <> wrote:
    > Darrell Grainger wrote:
    > >

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


    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
     
    William Ahern, May 7, 2004
    #17
  18. Alex

    CBFalconer Guest

    Guillaume wrote:
    >

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

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, May 7, 2004
    #18
  19. On Thu, 06 May 2004 19:02:29 +0100, Brian Gough wrote:

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


    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.

    --
    yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
    To email me, rot13 and convert spelled-out numbers to numeric form.
    "Makes hackers smile" makes hackers smile.
     
    August Derleth, May 7, 2004
    #19
  20. Mark McIntyre <> wrote:
    >On Thu, 6 May 2004 19:27:54 +0200, in comp.lang.c , "Frane Roje"
    ><frane.roje(*dele*te)@st.htnet.hr> wrote:
    >
    >>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


    .... 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
    --
    Irrwahn "writes x == 5" Grausewitz
     
    Irrwahn Grausewitz, May 7, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Nicholson
    Replies:
    3
    Views:
    745
    Rob Nicholson
    May 28, 2005
  2. Replies:
    0
    Views:
    2,464
  3. Replies:
    1
    Views:
    793
    Bertilo Wennergren
    Nov 24, 2003
  4. Hardeep Rakhra
    Replies:
    8
    Views:
    643
    Hardeep Rakhra
    Jan 15, 2004
  5. Ken Varn
    Replies:
    0
    Views:
    470
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page