Hacker News new | ask | show | jobs
by frogulis 17 days ago
Asking as an outsider: what's the issue with `| None`? Is it not type checked properly? Just prefer to do all error handling with exceptions? Other?
1 comments

It doesn't really constrain the range of Types that could come through so it's a tad bit harder to think about
I think exactly... and it can be "pervasive": you end up with special-casing None in a lot of places, when actually the "better" thing _could_ be to have a check once somewhere higher up the stack and dealing the None case there. Or (depending on how None is used) restructuring the code so there is never a None case. Often None signifies just "what happened to be coded up first" and that historical legacy can end up enshrined in the code structure for years.

It's not necessarily always "wrong", but you essentially have to think about "what's the right behaviour on None" a lot more, and while yes the type checker will help avoid runtime errors from random places, I think that can lead to essentially runtime bugs if you answer that question wrong. Or, at best you end up with quite messy data transformation and error handling, which itself can end up as a source of bugs.

One thing I try to ask myself is "what would this look like without `| None`", and take it from there. Also, it isn't `| None` as such, it can be any union type: code can end up with blurry responsibility.

> when actually the "better" thing _could_ be to have a check once somewhere higher up the stack and dealing the None case there.

This is what I tend to prefer. Not always possible, but worth striving for.

> One thing I try to ask myself is "what would this look like without `| None`", and take it from there. Also, it isn't `| None` as such, it can be any union type: code can end up with blurry responsibility.

Yeah, unions can be problematic, but at least they're more legible than None as this can basically break everything.