Thread: Comparison Question

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    Comparison Question

    Why is it fundamentally better to use the syntax:

    Code:
    if(42 == answer)
    as opposed to:

    Code:
    if(answer == 42)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It isn't.
    http://c-faq.com/style/revtest.html

    Get a compiler that warns you about using = in an if statement and move on with your life.

    This is an ancient trick that has only limited usefulness.

    For example, if you have
    if ( var1 = var2 )
    then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    It isn't.
    Question 17.4

    Get a compiler that warns you about using = in an if statement and move on with your life.

    This is an ancient trick that has only limited usefulness.

    For example, if you have
    if ( var1 = var2 )
    then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.
    QFT!

    Don't put the constant first!
    If you're conciously going to think about doing that every time then you may as well just conciously think about using the right operator every time instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I hate when people at work do this. It just looks annoying.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Quote Originally Posted by Salem View Post
    It isn't.
    http://c-faq.com/style/revtest.html

    Get a compiler that warns you about using = in an if statement and move on with your life.

    This is an ancient trick that has only limited usefulness.

    For example, if you have
    if ( var1 = var2 )
    then you're FORCED to learn how to use == properly, and no amount of rearranging the deck chairs on the titanic will save you.
    Or we can get even quirkier!

    Modified code from other help topic:
    Code:
    #include <stdio.h>
    #define equals ==
    void main(void){
      int one, two, three;
      printf("Enter 3 ints seperated by spaces:");
      scanf("%d %d %d", &one, &two, &three);
      ((one equals two) && (two equals three)) ? printf("Equal\n") : printf("Not Equal\n");
    }
    I wonder how many programmers just shuddered in fear.

    Oh, then later you can undefine "equals" and issue a vim command line ":%s/equals/==/g" and move on with our lives like nothing happened!

    Or we can avoid all of this and just remember "==" != "=" phew...
    Last edited by tenchu; 12-03-2010 at 12:39 AM.

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by tenchu View Post
    Or we can get even quirkier!

    Or we can avoid all of this and just remember "==" != "=" phew...

    That is funny.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > #define equals ==
    Guess what, people think that sucks as well.
    Question 10.2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    "equals" as macro name is a rather bad choice, perhaps "EQUALS" would be better? Anyway, this solves the problem of accidental assignment much better than reverse comparisons.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Quote Originally Posted by Salem View Post
    > #define equals ==
    Guess what, people think that sucks as well.
    Question 10.2

    Yes, my post was mostly meant as a joke but it does solve the problem. I think it makes more sense than the FAQ you linked and more than than switching the order of the comparison! At the end of the day the best thing you can do is just remember how to code properly and not make the mistake. I wouldn't rely on the compiler or any other convention to help prevent it. I hardly ever forget an equals sign when I compare, but I am much more likely to forget a semicolon when I shouldn't. :P
    Last edited by tenchu; 12-03-2010 at 11:55 AM.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I've actually seen several cases on this forum recently where people incorrectly had == instead of =.
    e.g.
    Code:
    found == true;
    Again, a good compiler comes to the rescue, noting that there is a statement that has no effect.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    I've actually seen several cases on this forum recently where people incorrectly had == instead of =.
    e.g.
    Code:
    found == true;
    Again, a good compiler comes to the rescue, noting that there is a statement that has no effect.
    I'm just remembering, with a shudder, some code from a colleague that had provided an operator==() for a class that had side effects - actually reading from a database. Apart from the fact he deliberately broke a "like int" guideline when overloading operators, he had sprinkled a few comparisons that had (it appeared) no effect into code that used the class. When those lines were removed by someone else, or turned into assignments, the program exhibited some strange bugs.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  2. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  3. easy comparison question
    By The Gweech in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2004, 04:37 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM