Re:CoffeeScript: less typing, bad readability
by evanmoran on Monday September 17, 2012 @03:51PM
(#4534505)
I find they are useful for shortcutting out of functions:
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.
[ Parent
| on HN ]
Re:CoffeeScript: less typing, bad readability
by nemetroid on Monday September 17, 2012 @05:19PM
(#4534946)
Though, what's the benefit of putting the conditional after? I find these:
if badness throw 'error'
unless goodness return null
to be more readable
Re:CoffeeScript: less typing, bad readability
by shardling on Monday September 17, 2012 @05:53PM
(#4535098)
It'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. :)
Re:CoffeeScript: less typing, bad readability
by nemetroid on Monday September 17, 2012 @08:56PM
(#4535715)
Those are good points. I'd probably make it multi-line:
if badness
throw 'error'
unless goodness
return null
Re:CoffeeScript: less typing, bad readability
by aes on Tuesday September 18, 2012 @02:01AM
(#4536599)
If 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()