10 ms·
One benefit to keeping your value wrapped in a Maybe is that as you transform and manipulate the value and pass it around in your system, you leave it up to the
by bern4444 5y ago
One benefit to keeping your value wrapped in a Maybe is that as you transform and manipulate the value and pass it around in your system, you leave it up to the last place in your system that uses the value to define the fallback value in the case of a None rather than defining a fallback value part way through and establish a convention that the fallback value means nothing was found at some other part of your system.
Another benefit to using Maybes is that you avoid the rigamarole of null checks at every call site where you want to use the value. If you have a function that returns null or a value, whenever you call that function you'll always have to add an if guard to validate it's not null. If it is, that function itself may return null, and callers to it will again have to implement the same check.
I wrote a JS implementation of the Option object and the readme has lots of specific examples about these benefits: https://github.com/sbernheim4/excoptional https://github.com/sbernheim4/excoptional and a few blog posts on these ideas[0][1]
[0] https://sambernheim.com/#/blog/the-engineers-schrodingers-cat https://sambernheim.com/#/blog/the-engineers-schrodingers-ca...
[1] https://sambernheim.com/#/blog/building-a-monad https://sambernheim.com/#/blog/building-a-monad