views:

272

answers:

8

Options A:

if (NULL == pSomethingColumn) // Yes, we use Yoda conditions
if (NULL != pSomethingColumn)

Or

if (pSomethingColumn)
if (!pSomethingColumn)

I am looking for references explaining the reasoning as well.

I have heard some people say that technically NULL does not have to be defined as 0, but come on! if that was the case, then the sucker (our app) would crash in -2147483648 different ways.

So, if NULL != 0, then we will have big problems.

Please help me settle a pointless syntax debate. I am not particularly attached to either way; just looking for the official way. Thanks.

P.S. We are using Visual Studio C++ compiler.

+8  A: 

Neither is clearly better than the other. From a technical standpoint they are equivalent. The number 0 in a pointer context does not mean all bits zero; it is defined to be equivalent to whatever bit pattern is used to represent null pointers. NULL can either be defined as 0 or (void *) 0. Either definition is syntactical and says nothing about the underlying bit representation.

So if (!p) is the same as if (p == NULL) is the same as if (p == 0) as far as technical correctness goes.

The issue is one of style. Like all style issues, your first inclination should be to match whatever the prevailing coding standards are. Do the same thing as the rest of the codebase. If there is no prevailing standard then it comes down to personal preference and you would be encouraged to develop a coding standard with your peers.

(Personally I like to use explicit comparisons and reserve the shorter syntax for boolean values, but I'll gladly use if (!p) that's what everyone else is doing.)

John Kugelman
+1 for the recommendation to follow the herd. This is actually the most important consideration when your code will be shared with a group.
Mark Ransom
This is C++, `NULL` cannot be `(void*)0` otherwise initializations such as `int *p = NULL;` would be illegal.
Charles Bailey
Also `if (!p)` is equivalent to `if (p == 0)` not `if (p != 0)`.
Charles Bailey
+11  A: 

I consider it an accident of history that zero corresponds to false and non-zero corresponds to true; I like to reserve those forms for boolean expressions only. However I don't have a problem with seeing that form in someone else's code.

My vote:

if (pSomethingColumn == NULL)

No, I've never been bitten by forgetting one of the equal signs. Just lucky, or maybe extremely careful.

Mark Ransom
Its also more natural to read this way and all modern compilers warn you if you accidentally do an assignment in an if statement (and since you (we/all of us) treat warnings as errors it will not compile) :-)
Martin York
@Martin York, do you happen to know if Visual Studio 2010 C++ compiler (unmanaged and managed) warn you as well?
Hamish Grubijan
@Hamish: yes, but only at warning level 4.
Mike Seymour
I learned this week that gcc 4.3 does not warn, if you do the opposite: The line `charArr[0] == ' ';` does not result in a warning. (We were hunting a "string is always empty" bug).
IanH
@IanH: Yes it does, with `-Wall` I get `warning: statement with no effect.` Clang also produces a similar warning.
dreamlax
+3  A: 

In C++ NULL has to be a null pointer constant in every implementation. 0 as a constant expression is always a null pointer constant (i.e. when converted to a pointer type it yields a null pointer value). Neither of these mean a null pointer value for any given pointer type has to be represented by an address that is zero or all bits zero but this is mostly academic from an application developer's point of view.

To use NULL you should #include a header that defines it such as <cstddef>; you can always use 0.

When converted to a bool a null pointer value always converts to false; any other pointer value always converts to true.

When used as an argument to unary ! a pointer value is converted to a bool as above and then negated.

So long as NULL is defined these are all equivalent:

if (NULL == pSomethingColumn)
if (pSomethingColumn == NULL)
if (0 == pSomethingColumn)
if (pSomethingColumn == 0)
if (!pSomethingColumn)

and these are all equivalent:

if (NULL != pSomethingColumn)
if (pSomethingColumn != NULL)
if (0 != pSomethingColumn)
if (pSomethingColumn != 0)
if (pSomethingColumn)
Charles Bailey
A: 

I use (!pSomethingColumn) because I'm lazy, but (pSomethingColumn == NULL) is more expressive as it shows the variable is meant to be a pointer.

DrDipshit
So does the fact that the variable is named `pSomethingColumn`.
dreamlax
@dreamlax, `pSomethingColumn` was given by me, the asker of this question. I have a habit of not revealing the exact variable names; that is how paranoid I am.
Hamish Grubijan
@dreamlax: That just means someone picked up a bad habit in the 1980s. Only the variable declaration will tell you what type it really is.
Mike Seymour
I have long said that the greatest crime in programming are committed attempting to save a few keystrokes -- just learn to friggin' type!
James Curran
@James: Agreed, but one should also avoid extra keystrokes that don't add anything to the code. To me, pseudoHungarian warts and explicit comparisons with zero (especially when backwards written) can slightly reduce clarity, by adding noise but little information. And sometimes, if the wart isn't updated when the type changes, or someone uses `NULL` when they mean `0`, such conventions become actively misleading.
Mike Seymour
+3  A: 

Call me eccentric, but I actually prefer one from each set. I would use if(ptr) if I'm about to do something that specifically requires the pointer to exist. I would use if(NULL==ptr) if I'm about to do something that specifically requires the pointer to be NULL. I guess this stems from the theory that it's easier to read statements that are not negated.

The more the merrier...if(!(0 != !ptr))

Marcin
Ah crap, you have just invented a third way.
Hamish Grubijan
personnally I tend to avoid the if(!ptr) because it happened more than once that I did not see the ! before ptr and so I guessed the test wrong. And when I see if(!(0 != !ptr)) I do a search in SVN to learn the name of the author, just to know if this psychopath is still around. :-)
Stephane Rolland
By the way you could always do more psychopatically unreadable, what do you think about that ? : if(!(0 != !(ptr++)))
Stephane Rolland
+1  A: 

You could probably find links to passionate arguments in favour of either one, but the only answer is: it doesn't matter. Both are correct, and either could be argued to be ever so slightly more expressive than the other, depending on your standards of expressiveness.

Regarding null pointers: while the representation of a null pointer is implementation-dependent, and isn't necessarily a value of zero, a null pointer constant is an integer constant with the value zero. So NULL must be defined as something that is convertible to an integer value of zero - the most obvious definition being 0.

Mike Seymour
Hm ... maybe I should have asked - what does Google / MSFT use?
Hamish Grubijan
@Hamish: Google's style guide doesn't go into that level of irrelevent detail. Microsoft have had all sorts of wacky conventions over the decades, but I'm fairly sure their current conventions also don't worry about details like this. An enlightened style guide should give rules that lead to maintainable code, not specify how to write code down to the last character.
Mike Seymour
@Mike, this is a good point. However, in the world of C# I happily follow StyleCop's rules which cover the most minute details, even if I disagree with 5% of them. I wish something like that existed for C++.
Hamish Grubijan
@Hamish: Such a tool would be useful for enforcing consistency, but (at least for me) that's a much less important quality than expressiveness and clarity. For the time being, the only tool that can check these is a human brain. Code reviews (or, at the very least, critical reading of your own code) will do more for quality than any style guide.
Mike Seymour
+1  A: 

I always prefer

  if (pSomethingColumn != NULL)

Not only because it is principally & conceptually correct, but also because it is self-documenting. Using a particular construct just because "it works" is a sure recipe for non-portability.

Android Eve
Thanks, and what is your take on Yoda conditions such as `if (NULL == x)` or `if (NULL != x)`?
Hamish Grubijan
Android Eve
+1  A: 

NULL is necessarily equal to 0 in any conforming C++ compiler. Personally, I prefer to use NULL because IMHO it's more explicit and the intent is clearer. But Bjarne Stroustrup uses 0 instead of NULL because he prefers to avoid macros. C++0x is going to have an explicit nullptr keyword that will hopefully eliminate this issue once and for all.

Chinmay Kanchi