|
This is bloviatory though. NULL/0 is an incredibly useful value, it is the simplest to test for in hardware, which is why it is the basis of booleans in every major systems language. Because null is used for boolean evaluation, null is the perfect value for a pointer which doesn't point to anything. Any time where you have an important distinction between the address of a valid object, and a non-address (next address at the end of a linked list, leaf node of a tree, failed initialization of a pointer). Zero is also used to terminate strings, for similar convenience/efficiency reasons. C compilers and static analyzers together tend to catch possible null dereference bugs with near certainty these days, so people don't tend to ship them these days, if they make any effort at all. I have not encountered a null pointer dereference which wasn't typo-related in... I don't remember the last time it happened. If you really want to be certain downstream users of your API won't struggle with it, put the null check in your sample code with a fat comment which says "This is NULL 0.001% of the time, and it really hurts when you don't handle that". ALGOL, of course, is the sort of language which doesn't have pointer arithmetic. I would agree that a language without pointer arithmetic should not have NULL pointers, if only because it doesn't make any sense for there to be an abstract reference to an object which can not be used with functions designed for it. In a language with integer pointers, like C, you check for null at allocation time. I've also seen people consider functions which could return NULL pointers to return something like an option type, where null is considered None, and everything else considered Some. |
I would argue than in at least 80% of the cases you don't want your values to be null, and that's in those situations mistakes are made (because you don't expect the value to be null !)