5 ms·
I find they are useful for shortcutting out of functions: throw 'error' if badness return null unless goodness Since it doesn't make sense to ret
by evanmoran 14y ago
I find they are useful for shortcutting out of functions:
throw 'error' if badness
return null unless goodness
Since it doesn't make sense to return or throw unconditionally it is makes it pretty clear that you should look for the if/unless.
- nemetroid 14y agoThough, what's the benefit of putting the conditional after? I find these: if badness throw 'error' unless goodness return null to be more readable
- shardling 14y agoIt's definitely nice to see the return at the beginning of the line -- makes the code easier to scan. And anytime there's code after a return, it had better be connected to a conditional. :)
- nemetroid 14y agoThose are good points. I'd probably make it multi-line: if badness throw 'error' unless goodness return null
- aes 14y agoIf may be strange because of the asymmetry but I'd actually prefer `if` to be come first all the time and `unless` to come last all the time - precisely because `unless` reads like it's for exceptional circumstances: value = cache[key] if value? return value return file.readContents() unless not file.exists()