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

From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 04/10/04


Date: Sat, 10 Apr 2004 00:33:58 -0400


"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. 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.



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. ... assignment within conditional expression" ... > makes the intention of the code crystal clear. ...
    (microsoft.public.vc.stl)
  • Re: what the author meant
    ... it should also be clear that all this stuff about authorial intention and mortification issues from something called "literary criticism", rather than say pro football or monopoly... ... You might surmise that Coetzee may have had such-and-such in mind when he was writing the book, e.g., race relations in South Africa. ... As a result of your reading the text, or I guess I should say listening to the text since it speaks for itself, you may reject your provisional hypothesis. ... I think you rejected my and Silke's interpretation that _Disgrace_ had something to do with race relations in South Africa, and you also rejected my suggestion that such a connection was intended. ...
    (rec.arts.books)
  • Re: what the author meant
    ... you read texts, construct an author from them, and then reapply that author to the texts as a strategy for reading the texts, why do it again?... ... other books of his to suggest that the doggie euthanasia had ... intention that might or might not be inferred... ...
    (rec.arts.books)
  • 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: You Failed David Friedman
    ... his clear intention here is to piss people off. ... but that isn't my reading. ... Zeborah and you and most of the others who took his work seriously and ...
    (rec.arts.sf.composition)