4 ms·
> If a developer doesn't check for a null value, are you really going to head to the documentation to find out why? Yes. I often write code in a way such that
by wolfgke 8y ago
> If a developer doesn't check for a null value, are you really going to head to the documentation to find out why?
Yes. I often write code in a way such that no null is checked. But if I do so, I always document it properly and my reasoning why, if the function is used properly, no null will be passed.
This way, you can be sure that if a null is dereferenced, not my function is the source of the bug, but a function that does not obey the pre-invariants that my function imposes on its parameters.
So you know that you are not supposed to "fix" this bug by introducing a null check to my function, but instead fix the bug in the other function that calls mine.
- dotancohen 8y agoI hope that you're leaving a comment in the function noting why you think that NUL should not be checked. Also, depending on the field, you might want to consider defensive programming.
- wilun 8y agoThe meaning of "defensive programming" is highly contextual even within a given "field". Also, major influential parts of the industry (last example in date being the C++ committee) are moving away from indistinct checks and exceptions, and toward contractual programming.
- tkxxx7 8y agoI mostly agree with this sentiment, but I find it a bit friendlier to other developers to assert(x not null) in most languages, at least so that they can find out sooner that the error is, in fact, their fault.
- laythea 8y agoHow do you know if you made a programming boo boo unless you do a null check... if (value == null) print "Must try harder" ?