Hi,
Could anyone please let me know which one is better to use and why?
ORCode:if(null == object){}
Code:if(object == null){}
Angkar
Hi,
Could anyone please let me know which one is better to use and why?
ORCode:if(null == object){}
Code:if(object == null){}
Angkar
I prefer
but that is only because I tend to use the assignement operator by mistake.Code:if(null == object){}
Kurt
Make sure you use NULL. null (all lowercase) is meaningless in C.
If you understand what you're doing, you're not learning anything.
Code:I prefer "if(!object) {}", simmply because it's shorter.
Last edited by King Mir; 05-23-2006 at 05: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.
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 tent do use
orCode: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 doCode:if ( Something )
etcCode:if ( Something == NULL )
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 02:09 AM.
Originally Posted by twomers
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...
http://c-faq.com/null/ptrtest.htmlOriginally Posted by twomers
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.*