3 ms·
not finding the requested record is a perfect case for an Either, or something that is potentially (legitimately) null. Thats not an exception generally.
by netcraft 7y ago
not finding the requested record is a perfect case for an Either, or something that is potentially (legitimately) null. Thats not an exception generally.
- tastroder 7y agoThat sounds like the perfect place for a single if (val === null) { handle that very predicable case and return } That whole example in the linked article displays coding style that surely makes sense but I can't imagine having that as a theme in my codebase. It's fringe and not native to the language, which makes onboarding others to that codebase a pain.
- netcraft 7y agoI dont disagree - where things like either, maybe or result really shine is when its baked into the stdlib _and_ there is machinery built into the language to make using them easy (like pattern matching, and them being in scope by default). I have used and written several libraries trying to make them work in js/ts and while they are beneficial around my custom domain specific code, you still spend a bunch of time wrapping things coming from libraries or other code you dont have control over. But the benefit of them over a check against null is that you can write a pipeline of functions that dont have to handle those cases and you know that its always safe to do so. This other article by the same author as the OP I think illustrates it well https://jrsinclair.com/articles/2016/marvellously-mysterious-javascript-maybe-monad/ https://jrsinclair.com/articles/2016/marvellously-mysterious...