Thread: Question - If(...)

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Question - If(...)

    Hi guys

    Is it true that writing

    Code:
    if(3 == a) {}
    is better than

    Code:
    if(a == 3) {}
    ?

    Thanx 'n greez

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I can't see why it would. You're not checking AND there, so the order shouldn't matter.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on how you classify better.

    It is "better" in the sense that it prevents the mistake of if (x = 3), because if (3 = x) is a compiler error.

    On the other hand, it doesn't work for if (x = y), because if x and y are both simple variables, if (y = x) is equally valid (and most likely incorrect).

    And many people argue that if you can remember to write if (3 == x) then you can remember to write two equal signs.

    It also looks weird doing if (3 > x) instead of if (x < 3)...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah but its kinda akward seeing those signs for me its better to use normal when you excerise alot you should never forget

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    ya, it may look awkward but believe me when you have a long code and by chance you make such a mistake it will take you hours to identify that ... where as in 3 == a.. compiler will make your life easy .. []

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by edesign View Post
    ya, it may look awkward but believe me when you have a long code and by chance you make such a mistake it will take you hours to identify that ... where as in 3 == a.. compiler will make your life easy .. []
    not as easy as if you stopped writing so much code before testing it
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    well, it happens to me... also a < 3 is ok , there s no benifit of writing 3>a, but in case of a == 3 it is the asignment , we are talking about ...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by edesign View Post
    well, it happens to me... also a < 3 is ok , there s no benifit of writing 3>a, but in case of a == 3 it is the asignment , we are talking about ...
    Agreed. The problem is that there are so many other situations where this can go wrong (x = = y is no better than y == x, if x and y are simple variables), and as Quzah says, it's often possible to test the code to determine that it does the right thing for equality tests.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    ya, this can help only if we are comparing with constants , so it is better to practice not making a mistake rather than practicing to write wierd looking expressions...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Another "besides" is that "trick" is about 30 years old, from when compilers only actually complained about errors, rather than things which might be suspect (the traditional job of lint).

    Modern compilers with a reasonable warning level will tell you about if ( a = 3 ) without any further prompting.
    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    Another "besides" is that "trick" is about 30 years old, from when compilers only actually complained about errors, rather than things which might be suspect (the traditional job of lint).

    Modern compilers with a reasonable warning level will tell you about if ( a = 3 ) without any further prompting.
    That obviously assumes that you either:
    a) have reasonable control over the compiler warning level.
    b) warning level is set to warn for these type of things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM