Coffeescript - just say NO!

July 15, 2013

Rails is now using "Coffeescript" rather than vanilla Javascript.

Coffeescript is a syntactical sugar coating on Javascript that some people like. I think it is stupid and needless, and there are plenty of people who agree:

I was open minded, and gave Coffeescript a whirl, but after giving it a good test run, I have decided that it adds virtually nothing and causes needless complication and headaches. The absolute worst thing about it is that it uses a pythonesque scheme of "meaningful white space", which is retarded and unforgiveable.

Javascript is a perfectly nice and readable language, whereas coffeescript is terse and cryptic.
I say that if it isn't broken, don't fix it -- or let me make an even stronger statement,
if it isn't broken, don't break it.

Ditching coffeescript in rails

I am working with Rails 4.0, which generates coffescript files by default. To get rid (yeah!) of coffeescript, you need to do two things: You don't strictly have to get rid of the gem, but if you do so, rails will stop generating stub coffeescript files when you generate controllers.

Ways in which coffeescript sucks

You say coffeescript isn't cryptic, consider this:

We use $ ->
which is shorthand for jQuery ->
which in turn is shorthand for $(function() { blah blah }); 
which in turn is shorthand for $(document).ready(function() { blah blah }); 

The absolutely most useful thing you can know about coffeescript is that you can use backticks to embed good old javascript into coffeescript files. So, where rails is expecting you to give it coffeescript, just use backticks and give it javascript!! Hurrah!

Some basics about coffeescript

A single pound sign serves as a comment

You get to use ruby style interpolation of variables into strings.

Coffeescript adds some cute looping constructs:

    print x for x in array
    print key,val for key,val of hash
These are nice, and coffeescript has invented a fancy name for them: "comprehensions".

Also coffeescript adds what it calls "splats", though it documents them poorly. All they are is a means to do a C-varargs kind of thing to gather up a variable number of arguments into an array. The syntax is three dots (...) rather than *x as in ruby. The following would gather up all arguments into a single array:

    (args...)->
Probably the most horrible thing coffeescript does is to make functions (particularly anonymous function) obscure and cryptic. Consider this perfectly fine javascript function (not anonymous):
function addem ( n1, n2 ) { return n1 + n2; };
In coffescript this becomes:
convert = (n1, n2) -> n1 + n2

In coffeescript the words "var" and "function" are illegal, as are semcolons. Not only that, whitespace is significant (the horror!). The vague concept about whitespace is that you can replace semicolons with tabs. Pretty much the same brain damaged mess as python. Excuse me while I go puke.


Feedback? Questions? Drop me a line!

Ruby on Rails notes / tom@mmto.org