Re: Style question: Should one check for NULL pointers?

David D. Hagood
2003-07-10T22:39:53Z

There is an old saying in software design:

"Never check for an error condition that you do not know how to handle."

In other words: if you have identified a possible error condition (such
as a NULL pointer), until you have identified a way to meaningfully
handle that error condition, simply testing for it is useless.

Now, if you have some function that can return an error code, then
testing for NULL and returning an error condition is sensible. But if
you have no way to report the error, then what good is the test?

However, if you test for NULL, and log a report when you detect it then
deref it anyway (to force an OOPS, in other words throw an exception),
then at least there is some meaningful info in the log.

But just doing something like

void foo(void *ptr)
{
if (!ptr)
return;

....
}

just masks the problem.