CoffeeScript Style Guide [closed]
开发者_运维技巧
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionI've been trying to apply PEP8 as closely as possible to CoffeeScript.
Are there any other coding conventions you follow?
I use the postfix form of if/unless only for guard constructs:
return if not valid
break if finished
continue if not important
Not for assignments:
mood = greatlyImproved if singing
My reasoning is based on the condition being hidden off to the right and a control flow path being on one indentation level.
When I look at a block of code, I can scan down the left and can see the control flow. Code which follows a return is obviously only reachable if the return only happens sometimes, so it stands out. It's a recognizable pattern and having it in one line is better than two.
An assignment doesn't stand out however and it's easier to overlook the condition on the right. If an assignment only happens sometimes, I think an if with indentation is clearer:
if singing
mood = greatlyImproved
精彩评论