13 ms·
Program logic fundamentally has to contend with different conditions. Sometimes the user will be logged in and have friends, sometimes they won't. The "maybe"
by lixquid 3y ago
Program logic fundamentally has to contend with different conditions.
Sometimes the user will be logged in and have friends, sometimes they won't.
The "maybe" style has the inconsistency embedded in the type system; it's
impossible to have an invocation to getFriends and then not handle the
resulting possibility of not being logged in.
Shifting it up to the caller just means that you're going to have to remember
to ensure the user is logged in before calling getFriends otherwise you'll get
some kind of error, which might give you more control, but now there's no
guarantee in the type system that you've handled the case where the user isn't
logged in.
Writing ifs everywhere to handle failure conditions might be a bit of a pain,
but that's more of a failing of the language than the style.
- nnnnico 3y agoI agree with this. I prefer explicit optional/null return types, otherwise the possibility of an error still exists but is now hidden to the caller. Langs with syntax for concise optional chaining and such fix this problem imo
- madeofpalk 3y agoNote that Typescript - the language of the examples - does have proper null through it's type system. A function that returns `T | null` will require callers check for null before using T.