Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Bo Persson (bop_at_gmb.dk)
Date: 04/10/04


Date: Sat, 10 Apr 2004 11:54:31 +0200


"SaltPeter" <SaltPeter@Jupiter.sys> skrev i meddelandet
news:CMKdc.16559$BF2.1319444@news20.bellglobal.com...
>
> "Rob Vermeulen" <rvermeulen@arbor-audio-antispam-.com> wrote in message
> news:107dahl52n7ch3f@corp.supernews.com...
> > In several SDK examples of MS I notice the following.
> > To compare a pointer with NULL they do: "NULL == pMyPointer"
> > Why not just the other way around, like every "C++ for dummies" book
> > describes? :-)
> >
> > Is this some sort of optimisation ? (Can't imagine it is, actually,
since
> in
> > ASM it does not matter what operand you take first)
> > I was just curious if there is a sneaky reason why they do that.
> >
> > With regards,
> >
> > Rob.
> >
>
> As already mentioned, its a question of style. The primary reason is to
> announce your intentions to both the compiler and anyone reading your
code,
> including yourself. Note that if you wrote the following by accident:
>
> if(pMyPointer = NULL)
> {
> // whatever
> }
>
> The compiler won't detect the error in the code.

But it will!

Your code produces "warning C4706: assignment within conditional expression"

>And someone reading the
> code won't recognize without further inspection that your intention was to
> do a comparison.
>
> if(NULL == pMyPointer)
> {
> // whatever
> }
>
> makes the intention of the code crystal clear. Since NULL is a constant
only
> a comparison is allowed, no assignment possible.

The intention may be clear, but the code is ugly enough that people have to
ask news groups why the heck it is written this way. :-)

Not recommended.

Bo Persson



Relevant Pages

  • Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"
    ... > I was just curious if there is a sneaky reason why they do that. ... The compiler won't detect the error in the code. ... And someone reading the ... makes the intention of the code crystal clear. ...
    (microsoft.public.vc.stl)
  • Re: hp-ux compiler
    ... >> without any intention to break any laws. ... intention of exploiting it comercially, there is not one law broken. ... > Some think it not strange to scam money off old people. ... You didn't want to say which compiler it was because you ...
    (comp.lang.cobol)
  • Re: A very **very** basic question
    ... Whichever way you look at it i is set to the value 0. ... this is known as assignment. ... And I dont see any reason to confuse a ... certainly weren't invented with the intention of confusing people. ...
    (comp.lang.c)
  • Re: Memory regions
    ... Any commands? ... My intention is to check for the variables defined in the ...
    (comp.unix.programmer)
  • Re: What is the advantage of "NULL == pMyPointer" instead of "pMyPointer == NULL"
    ... The primary reason is to ... >> The compiler won't detect the error in the code. ... Point was that its a warning, not an error and detecting what the code ... >> code won't recognize without further inspection that your intention was ...
    (microsoft.public.vc.stl)