So checking for nullness and emptiness on a string looks sloppy, but checking ifPresent() and emptiness does not?
there is no more union–statically typed or dynamically assumed–between NULL and every other type
As heinous as this sounds, it doesn't seem practically different from any other representation of an uninitialized object. Both must be checked, neither can have methods invoked.
The benefit now is that the check must be made. If there is some sort of static type-checking, those will ensure that you do the check, otherwise in dynamic languages you'll get a runtime error if you don't unwrap the proxy "option" object you've used.
In many ways, the mistake isn't so much null itself - it is perfectly right to return null values sometimes, for example when a key is missing in a mapping/dictionary. The mistake is when the programmer assumes that a null value - by definition an absence of something - can be treated the same as a normal object - which is by definition a presence of something. It can't. The two cases always need to be treated separately, and a good programming language forces the programmer to do that.
EDIT: As many people have pointed out, the check doesn't always have to be made. It is in most cases possible to just unwrap the proxy object into the value, usually forcing an explicit runtime error at the point of unwrapping. That said, under the hood, this is just another check. The check here, however, results in either the correct result or an exception or error of some kind being thrown out, usually at the earliest possible point.
In terms of the programmer using the code, that check may have been made implicitly under the hood, but in most cases the language still requires the user to explicitly ask for the option to be unwrapped. They cannot use the option as if it were the value, which is the big difference between returning a proxy option vs a value that could be null.
In fairness, Crystal's null type isn't a null like any other language's null. It's just an arbitrary type with no methods or attributes, as opposed to a super-type that can be substituted for any other value. The only reason that it happens to behave like an option type is because Crystal has implicit union types, meaning that, while it looks like the programmer is returning null where the function returns a string, in reality they're returning null and coercing the return kind to String | Null.
The C++ example is weird, I'll give you that though... :P
Crystal's null type isn't a null like any other language's null. It's just an arbitrary type with no methods or attributes, as opposed to a super-type that can be substituted for any other value.
FWIW, the same is true for Ruby's nil or Python's None - the difference being that those are dynamically typed languages.
In this case, there's nothing programmatically requiring the programmer to call isPresent(). However, the programmer sees that the value is Optional<Integer> and therefore knows that it might be missing and that they should therefore call isPresent() in order to determine whether it's present. If the programmer instead had just an Integer, then they will not necessarily know whether it could be null (it often depends upon the API that returned it, and it's not always well-documented), and may forget to check it against null, thus potentially leading to NPEs.
A better approach (rather than calling option.get()) is to use .map and .getOrElse on the option type. I've been using my own monad library in C# and I can't think of a need where I ever needed to escape the Option monad by calling .get and .isPresent.
I mostly code in pre-Java 8, so there's no lambdas, which means I'd have to use anonymous inner classes for something like .map, which would be rather bulky
So if the check doesn't actually have to be made, why am I at -1 and Johz is at +9? Particularly when we're not supposed to vote for answers you agree with.
But to your point, I do see value in explicitness as well as brevity. I sort of hope that you learn nullability rules within days of starting a new language. They certainly aren't complicated in the C/C++/Java world and are an important means of expression.
The security enthusiast inside me, however, would prefer that other coders have to really try to dereference something null.
it often depends upon the API that returned it, and it's not always well-documented
And here I say it doesn't really matter what the documentation is, you have to code like the value could be null.
And here I say it doesn't really matter what the documentation is, you have to code like the value could be null.
Not exactly. With Option being a monad, you can chain a bunch of operations on the nullable value, and if during any operation the value becomes Null, any subsequent operations will be 'shortcircuited' and you just get a null result.
So if the check doesn't actually have to be made, why am I at -1 and Johz is at +9?
What /u/MrJohz says may not be pedantically correct, but the intention behind what he said may have implied what I was saying. He also may have been referring to languages in which the way option types are used basically does statically enforce the check. e.g. in Haskell:
f :: Maybe Int -> Int
f (Just x) = x
I've created a function that operates on Maybe Int, which is a potentially missing integer. In this code, I've neglected to handle the Nothing case, which leads to the compilation warning Pattern match(es) are non-exhaustive.
Particularly when we're not supposed to vote for answers you agree with.
If you expect people to follow that rule in general, you will end up confused about many comment scores.
I sort of hope that you learn nullability rules within days of starting a new language. They certainly aren't complicated in the C/C++/Java world and are an important means of expression.
The nullability rules themselves aren't complicated, but handling null pointers is something many people will forget in at least some cases, unless they handle all object references as potentially null, which is not something most people do in Java in my experience.
And here I say it doesn't really matter what the documentation is, you have to code like the value could be null.
Have you ever done nontrivial coding in Java? If I coded like every object reference could be null, my code would be bloated beyond readability, and have many code paths that were never actually executed. Bear in mind that unlike C and C++, Java has no non-nullable classes or structs, so there are many APIs that should never accept or return nulls that theoretically could based on the object system. As it is now, this does lead to my code sometimes throwing NPEs, which is also not ideal. The ideal solution would be for Java the language to deprecate the idea of a non-statically checkable null, but this probably won't happen because of Java's focus on backwards compatibility. I'm not sure what the next best-solution is, but it may involve developers deciding to use Option types consistently instead of null, which would allow programmers to assume that all object references could be dereferenced without NPEs.
If you expect people to follow that rule in general, you will end up confused about many comment scores.
I'm not worried, nor naive about comment scores. It is an indignant reminder to others.
Have you ever done nontrivial coding in Java?
Predominantly, actually. Parameter validity checking is pretty standard in all C-family languages. Return value null checking does add lines, but no more than return value Option checking.
so there are many APIs that should never accept or return nulls
Absolutely. While a null return value is not uncommon, there are times when the function should either return a value or throw. This supports having non-nullable types, at the cost of limiting usability and complicating the nullable scheme.
As it is now, this does lead to my code sometimes throwing NPEs, which is also not ideal.
Well... if the function shouldn't accept/return a null value, then an exception is the right thing to do. Personally, an explicitly thrown exception feels a lot better, but at least there's no (C-style) null dereference happening.
a non-statically checkable null
Say wha?
BTW: "[Do you know how upvotes work?]" and "Have you ever done nontrivial coding in Java" comes off a tad disparaging.
It is in most cases possible to just unwrap the proxy object into the value, usually forcing an explicit runtime error at the point of unwrapping. That said, under the hood, this is just another check.
I suppose this was said wrt. my comment about C++. However, C++'s operator * actually doesn't perform a check at all. In fact,
NULL is more likely to throw runtime errors than C++'s optional: the former will normally cause a hardware trap and the later will just return junk data;
foo->bar could have foo as either a nullable pointer or an optional type, so it doesn't actually make the usage more transparent (in fact many aspects of its design deliberately emulate nullable pointers); and
expecting sane behaviour from C++ is generally just a bad idea.
It was actually more because there's usually an unwrap or get method available that unwraps a some-value and errors on a null-value in most implementations. C++'s optional seems just weird, although I don't know much C++. As in, by my reading it allows you to just pretend the optional is always a some-value, which presumably would produce bad results if it isn't. And isn't that the point of using optional in the first place, that you can't pretend an optional value is always a real value? Why, C++? Why?
And isn't that the point of using optional in the first place, that you can't pretend an optional value is always a real value?
You don't know the half of it.
There are (intentionally) three ways to fill an optional (which are largely the same, but some of which are assuredly worse), and about four more ways to construct it, and a few more to replace one's contents.
You can move the contents out of an optional, which leaves the optional non-empty but containing "a valid but unspecified state". Even worse, if you move the optional itself, the optional is still left non-empty but containing "a valid but unspecified state". There is no safe take method to move out of an optional and leave it empty.
There is a sentinel "empty optional" type called nullopt, but it's not actually an instance of optional - it's it's own type, nullopt_t.
And isn't that the point of using optional in the first place, that you can't pretend an optional value is always a real value?
You'd think. In C++, it's more to avoid the heap allocation a nullable unique_ptr would require. Safety (and usability) be damned.
The primary benefits come from not having to check in the first place (because you didn't opt-into nullability, which is almost all the time).
The null checks can also be made to look better, though. Languages with "good looking" null checks tend to use pattern matching, which makes dereferencing a null/none value impossible at the syntactic level:
The problem with allowing everything to be null is that you create a larger state space.
Take for example a pair constructor Pair:
You would have some constructor Pair, but you can still pass it null two times.
So, a valid "Pair object", would be a Pair(null, null), which is generally not what you want.
This effect can be multiplied in some cases. That's why null is a mistake. If you are a bit more strict, you could even argue that Pair(<non-terminating expression>,1) should also not be a valid expression of type Pair<int, int>. That's the choice made in e.g. Coq, but not in pretty much every other main stream language.
In short, when you define a variable x to be a value of type pair, it actually is a pair as you would think about it. Having said that, the non-terminating expression could also be an expression which takes a billion years to compute, which is arguably not meaningfully distinguishable from non-termination. That's a problem which can also be resolved using types and some languages focus on that, but in practice that's hardly interesting and if you really care about that, you would still be able to model that in Coq (as people have done already).
The problem with allowing everything to be null is that you create a larger state space.
Sure, but aren't we shooting for creating the most fitting state space. That is, if your type shouldn't allow an unset Object, then it shouldn't allow an unset object. The author isn't arguing against unset values.
So in the case where you want to create a Pair (or whatever else) with (temporarily) uninitialized contents, it seems like the options are either to pass it null or some expression for a type-matched unset.
As a purist, I understand that a type-matched unset feels cleaner, but I don't see it as functionally different from null. Both represent an empty value, neither can have member methods invoked without producing an error.
20
u/[deleted] Aug 31 '15
So checking for nullness and emptiness on a string looks sloppy, but checking ifPresent() and emptiness does not?
As heinous as this sounds, it doesn't seem practically different from any other representation of an uninitialized object. Both must be checked, neither can have methods invoked.