46 ms·
To each their own. To me return !!val ? val \* 10 : val; and return val.map(x => x \* 10); are equivalent. They're both confirming the presence of a val
by Veuxdo 5y ago
To each their own. To me
return !!val ? val \* 10 : val;
and
return val.map(x => x \* 10);
are equivalent. They're both confirming the presence of a value before using it. And callers of both main/mainWithOptions will have to do likewise.
> Returning None more clearly demonstrates intent as well when you really don't want to return anything (but want to avoid returning null/undefined) and avoids having to come up with conventions for returning specific values that signify null/undefined by callers.
Null and undefined are values that are used for this exact purpose, though...
- bern4444 5y agoThe difference here is callers of mainWithOptions won't have to do the null/undefined check again. They get back an option so they can just keep calling map. And that's true all the way up the call stack. But in the main example, since it may always return null/undefined, every caller up the stack will also have to do this check. Null and undefined are used for lots of different purposes by lots of different people. None (and Options more generally) is much more specific - there may or may not be a value here but you can safely work with this value regardless without needing to first check. The checking is built into the object for you. Neither way is right, just about what you prefer. I find the ergonomics of options much nicer than null/undefined.