|
|
|
|
|
by lmm
4913 days ago
|
|
The difference is that you can distinguish at the type level between values that could be "null" (Optionals) and values that are never null, and the two look different from each other. Otherwise you tend to have one of two problems: a) Every function checks all its parameters for null, and you do null checks before calling a method on any object, wasting time. b) It's down to the programmer to figure out where a null check is necessary and where it isn't, and they sometimes get it wrong. (Of course, better code will avoid calling isDefined or get, but even if you just use it as a like-for-like replacement, using optionals in place of null can improve your program) |
|
If your code doesn't check for a null, JIT adds in an implicit check in case it needs to throw a NullPointerException. When you put a check in your code, it knows to eliminate the implicit check.
Putting null checks everywhere does lead to ugly, complicated-looking code though; it'd be nice if there were a simple syntax for non-null parameters.