Fun with NULL pointers, part 2
Fun with NULL pointers, part 2
Posted Jul 22, 2009 9:15 UTC (Wed) by epa (subscriber, #39769)In reply to: Fun with NULL pointers, part 2 by i3839
Parent article: Fun with NULL pointers, part 2
Either make sure that all callers don't pass in a NULL pointer, or if that isn't possible, add a check and return with an error.Suppose you take the first option; you check all the code that could call your routine and make sure NULL is never passed. Then there is no need for a runtime check, right? In theory this is true but I know I make mistakes, so even after checking all the code I would still put in the check for this 'impossible condition' to stop a small oversight causing a big problem.
It's the same argument for any assertion you put in your code: since they are checking for something that can never ever happen, why bother with them at all? All programmers make mistakes, but the better ones are aware of this fact and take appropriate measures.
(This is not an argument for sloppy 'defensive programming' as sometimes practised, where you insert code to silently return the wrong answer or do something random if the inputs aren't as you expect. You want a macro like BUG_ON or assert() which loudly announces the problem, not hides it.)
