top | item 2725006

CoffeeScript. An Outsider Opinion

17 points| nuclearsandwich | 14 years ago |blog.nuclearsandwich.com | reply

28 comments

order
[+] dualogy|14 years ago|reply
People ask time and again, and here again: do we really get more productive by saving curlies and semicolons? Really?

YES:

It's not the time saved while writing the code that matters here -- although I do believe it adds up quickly if you work on a project that might occupy you for months or years and have invested the initial week of getting fully into CoffeeScript mode.

NO, it's the time saved reading your code time and again as you revisit your code-base over these months or years of refining your project going forward.

CoffeeScript's "easier writability" may be debatable, but it's its "easier readability" where it really shines. I can glance quickly at my way-fewer-lines of CoffeeScript and parse it much more smoothly than I ever could a curly C-style language. Maybe it's because I first started out in, boohoo, BASIC. But indented lines with no superfluous { syntax; decorators } just flow into my brain much faster.

If you're a fire-and-forget coder who writes line after faultless bugless line that you never need to revisit, review or simply recall and still get a meaningful composition of a program, app or site that isn't just a house of cards built on quick-sand or a simple batch job at the end of the day -- I envy you! In my case, 99% of my classes and functions are an API to each other. So I look up how the stuff I wrote days, weeks or months ago was supposed to be called or initialized constantly. I read my code more often than I write it.

I like writing CoffeeScript but what matters is -- I love reading it. Before, I liked writing JavaScript -- but I hated reading it.

[+] jashkenas|14 years ago|reply
Quick point of clarification. The post says:

    > As it turns out that isn’t entirely true since coffeescript 
    > is a class based object oriented language and javascript is 
    > prototype based. This was actually a mark against 
    > coffeescript for me since part of my desire to learn 
    > javascript was to dally in prototype based OOP.
CoffeeScript is prototype based to the precise same extent that JavaScript is. The "class" keyword is just sugar for JavaScript's constructor function + prototype chain combination. To mangle Shakespeare:

What's in a name? That which we call a class by any other name would smell as sweet.

Call it a prototype if you like -- it's the same thing in code.

[+] nuclearsandwich|14 years ago|reply
thanks Jeremy, do you mind if I insert your comment in the main body of my post?

I am still a student of computer science and in my university's Java encapsulated worldview they've not discussed the finer points of non class-based OOP. I will do more research on the subject.

[+] Almaviva|14 years ago|reply
Do semicolons really distract people from thinking about the behaviour of their code? Really? Would these people like their novels and articles decompiled so that every sentence is on a new line, and thus periods wouldn't distract them so much from the meaning of the text? Or is it just transparent after a while, as the way you end something?
[+] hammerdr|14 years ago|reply
Books and code have different usage patterns.

In books, you typically start from the beginning and read to the end. You consume the book serially and the only breaks you are concerned about are pauses (period, comma, colon, etc.) and page breaks (chapters).

In code, you typically scan. Scanning a book is a painful experience. The sentences never start in the same spot. In code, the heirarchcal structure and text layout helps for scanning and identifying quickly. As someone said previously in this thread, cookbooks are done the same way and for the same reasons.

[+] cageface|14 years ago|reply
I'd prefer a Coffeescript that restored semicolons and braces and didn't depend on indentation to denote block structure. Fixing some of Javascript's most serious warts is a great idea but, IMO, significant whitespace is a bad idea here just as it is in Python and Haskell. It just makes routine code manipulations like copy & paste and block refactoring a lot more tedious.
[+] mburns|14 years ago|reply
>Would these people like their novels and articles decompiled so that every sentence is on a new line

Isn't that how we write cooking recipes?

[+] nuclearsandwich|14 years ago|reply
I see your point. Punctuation is a vector for meaning and clarity but is Javascript's syntax any more informative than Coffeescript's? It isn't in my observation. It certainly offers more flexibility in style, but why would you need it? I can't conceive of a reason besides minification.

Your comparison with novels I think is a bit skewed. Literature has a whole different set of readability enhancements such as fully justified text, variable-width fonts, as well as margins and padding. Punctuation in English has been evolving for centuries as people settle on the minimum needed to convey the full meaning as unambiguously as possible. But English also has many many more sets of valid (as well as various definitions of valid) constructs than Javascript does and thus may require a more robus set of punctuation.

[+] heyrhett|14 years ago|reply
I'll use anything that fixes the way javascript handles scoping. Take this gem, for example:

<html> <body> <ul> <li>First</li> <li>Second</li> <li>Third</li> <li>Fourth</li> </ul> </body>

<script type="text/javascript"> els = document.getElementsByTagName('li'); for(i=0; i < els.length; i++){ els[i].addEventListener('click', function(){alert(i);}, false); } </script> </html>

[+] pdenya|14 years ago|reply
Javascript has a few gotchas like this one. These are annoying for people new to the language but after knowing they exist they're easily avoided.
[+] pdenya|14 years ago|reply
CoffeeScript has some nice benefits but in my opinion not having to deal close curly braces or add semi colons hardly qualifies as a reason to use one language over another.
[+] nuclearsandwich|14 years ago|reply
I'm still using Javascript at the end of the day, I'm just doing so via an interface that I find more readable and writeable. I don't see it as much different than letting a minifier optimize my javascript rather than writing it that way by hand.
[+] cdcarter|14 years ago|reply
Though I love CoffeeScript, I feel it's very important to understand how JavaScript works. It's a great language, but only if you understand what is going on, and why you do have to use hasOwnProperty.
[+] nuclearsandwich|14 years ago|reply
I'm working on it. I did mention that I'm learning Javascript at the same time. I should add Crockford's JavaScript: The Good Parts to my coffeescript resources list. Can you link me to an article explaining hasOwnProperty?
[+] fleitz|14 years ago|reply
Personally, I really like the whole coffeescript/sass/haml trio. (I really don't like SCSS).

It drastically reduces the amount of errors in your code, at the expense of a syntax that's a little different.

[+] nuclearsandwich|14 years ago|reply
Yeah, you've hit on what really appeals to me about those three and others like them. They shift focus to what's really important in each case.

In HTML I want to focus on document structure, not closing my tags. In CSS I want to focus on style and visual consistency, not nesting. In Javascript I want to focus on application behavior, not semicolons.

[+] dualogy|14 years ago|reply
Last time I looked into sass/scss/less, I decided Stylus is for me. Looked into that? Not just getting rid of semicolons and curlies, but even colons :) plus macros, functions, variables, calculations, all the "usual" stuff.
[+] lucian1900|14 years ago|reply
You might also like LESS.