Hacker News new | ask | show | jobs
by disgruntledphd2 18 days ago
Yeah, that always stinks of types look nice, but I can't be bothered to try a little harder.
1 comments

What do you think it should be? I use | None a lot and it doesn’t seem like a smell to me
i feel like you probably just shouldn't return None, as it causes issues when tracing problems through the system.

If you must return None somewhere, I'd really prefer it to happen in one place (so the top level function may return None if something weird happens, but all the individual functions return a value).

But to be fair, this is probably just trauma from tracking down weird sentinel values.

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?
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.

Ah right, I didn’t consider this was about return types! I agree that returning None is often a smell.