by CookWithMe · 14y ago · 0 comments
You would only check for nulls "everywhere" if all functions could potentially return null. Then, indeed, there is no difference. But (hopefully) not all functions will return null, so you'll probably have at most a single-digit percentage of functions that do.
The point is that by using Option, you are explicitly stating "This function can return null", making it impossible for the caller to ignore. If you write it somewhere into the docs, it is indeed easy to overlook.
This may not be as relevant when you are only working with your own code, but I find this (and static typing in general) most helpful when dealing with code from someone else, including the libraries one uses.
In a real world API, almost everything that returns an object can return null or throw an exception.
> The point is that by using Option, you are explicitly stating "This function can return null", making it impossible for the caller to ignore.
Throw an exception if impossible to ignore is the motive.
> If you write it somewhere into the docs, it is indeed easy to overlook.
It might be overlooked. But is it too much to assume that someone making a call will check the parameters and the return type?
> It might be overlooked. But is it too much to assume that someone making a call will check the parameters and the return type?
http://news.ycombinator.com/item?id=4695587
Apparently it is too much to ask, even if the program is performing something super-important for security like SSL.
The whole discussion started from the claim that Haskell makes the NullPointerException/NoneType non existent. And I am just saying it isn't any different from how you enforce it in any other language - you either handle nulls or throw exceptions.
> (so throwing an exception is wrong)
I am sorry, but I don't play the "throwing an exception is wrong" game. I use it for actual exceptions, control flow, must-handle scenarios. I don't see how exceptions are defined has to do with how I use it if it makes my program logic clear or translates to my intent. The only reason I think before using exceptions for things which aren't exceptions is the stack which is saved, which most of the times so minuscule that it doesn't matter. Ruby has the best compromise as in it defines catch..throw; most of the languages don't so I resort to using exceptions.