3 ms·
One is that it's way too easy to forget to check for NULL. Null-checks only make sense when you expect a null value. If you don't, and you get one, then either
by userbinator 11y ago
One is that it's way too easy to forget to check for NULL.
Null-checks only make sense when you expect a null value. If you don't, and you get one, then either your code should be expecting one and has to do some processing differently, or something else is broken.
If the answer is "no," what happens if you pass NULL anyway? The compiler certainly won't catch it, and who knows what will happen.
In all likelihood, a crash when it tries to do something with whatever the pointer should be pointing at, in which case you should be asking why your code is giving it a null.
- mikeash 11y agoAnd when do you expect a NULL value? If you use an optional type, then you expect a NULL value if and only if the type in question is an optional. If you forget the check, or if you check a value that can't be NULL, the compiler will tell you. Beware programming errors which reveal themselves as "in all likelihood," because that hides an entire universe of difficult-to-debug behavior, and sometimes behavior that can be subverted. Optionals solve these problems, and solve them far better than the "be careful not to do that" approach you describe here.