which one is better?

This is a discussion on which one is better? within the C Programming forums, part of the General Programming Boards category; Hi, Could anyone please let me know which one is better to use and why? Code: if(null == object){} OR ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    which one is better?

    Hi,

    Could anyone please let me know which one is better to use and why?
    Code:
    if(null == object){}
    OR
    Code:
    if(object == null){}

    Angkar

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I prefer
    Code:
    if(null == object){}
    but that is only because I tend to use the assignement operator by mistake.
    Kurt

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Make sure you use NULL. null (all lowercase) is meaningless in C.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    I prefer "if(!object) {}", simmply because it's shorter.
    Last edited by King Mir; 05-23-2006 at 06:50 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    1. Good compilers will spot if ( object = null )
    2. if ( null = object ) style is counter-intuitive from a reading style point of view.
    3. if ( obj1 = obj2 ) cannot be made safe by any amount of rearranging, so your safety net has been cut.
    4. Particularly stupid zealots apply the same rule to other ops as well, like if ( 4 < x ), but sometimes they just swap the values round without changing the operator. So instead of improving things, they introduce bugs instead (yes, this really has happened!).

    http://c-faq.com/style/revtest.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I tent do use

    Code:
    if ( !Something )
    or

    Code:
    if ( Something )
    partially because it's shorter, and partially because of some other reason I haven't thought of yet. But before I did that I use do

    Code:
    if ( Something == NULL )
    etc


    EDIT:


    Actually, on that note, does if ( !Something ) evaluate to if ( NULL == Something ), or if ( Something == NULL ) ?
    Last edited by twomers; 05-23-2006 at 03:09 AM.

  7. #7
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by twomers
    Actually, on that note, does if ( !Something ) evaluate to if ( NULL == Something ), or if ( Something == NULL ) ?

    My understanding is that it takes whatever value is in Something and inverts it. I think inverts is the right word...
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by twomers
    Actually, on that note, does if ( !Something ) evaluate to if ( NULL == Something ), or if ( Something == NULL ) ?
    http://c-faq.com/null/ptrtest.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21