Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript Style Guide [closed]

Tags:

coffeescript

I've been trying to apply PEP8 as closely as possible to CoffeeScript.

Are there any other coding conventions you follow?

like image 325
mbarkhau Avatar asked May 24 '11 12:05

mbarkhau


People also ask

How do I run a CoffeeScript in browser?

Load a remote script from the current domain via XHR. Activate CoffeeScript in the browser by having it compile and evaluate all script tags with a content-type of text/coffeescript . This happens on page load. Listen for window load, both in decent browsers and in IE.

How do I download CoffeeScript?

CoffeeScript with Node and npm msi on Windows and install node from http://nodejs.org/dist/v0.6.6/ — See http://nodejs.org/dist/ for newer versions. Open a command line. On Windows 7 right-click and select 'Run as Administrator' when starting the terminal. On Mac OS X you can use 'sudo' when starting the npm command.

When was CoffeeScript created?

CoffeeScript was created by Jeremy Ashkenas and first released on December 25, 2009. The original compiler was written in Ruby. The compiler was rewritten in CoffeeScript and released in March 2010. For its syntax CoffeeScript borrows heavily from Ruby, Python, and of course JavaScript.


1 Answers

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
like image 163
mbarkhau Avatar answered Oct 02 '22 14:10

mbarkhau