

CoffeeScript. An Outsider Opinion - nuclearsandwich
http://blog.nuclearsandwich.com/coffeescript-an-outsider-opinion

======
dualogy
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
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
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.

~~~
jashkenas
Go for it. In a nutshell:

An object is a unit of code and data that can be treated as a distinct entity.
Classes and prototypes are both way to make objects. With a prototype, you
start with an actual object as an example, and make more objects just like it.
With a class, you start with the abstract set of properties that describe a
type of object, and make more objects from that blueprint.

Think of it as the Platonic ideal of a thing (the class), versus the canonical
example of a thing (the prototype).

To get specific: The idea of Bicycle, versus my blue bike with red streamers.
Both can be used to make more bikes, in JavaScript.

    
    
        newBike = new Bicycle
        newBike = Object.create(myBlueBike)

~~~
nuclearsandwich
Awesome. Thanks for that. I've always enjoyed the link between Platonic forms
and object Classes.

ahhhphilosophy.jpg

------
Almaviva
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?

~~~
cageface
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.

~~~
jashkenas
There are many other compile-to-JS languages that might be more to your
liking. Perhaps try Objective-J? It's a strict superset of JavaScript.

<http://altjs.org/>

~~~
cageface
I already curse Objective-C for its verbosity, so that's not the right
direction for me. Coffeescript is in the right ballpark, I'd just rather have
a more explicitly structured syntax, perhaps closer to Ruby.

------
heyrhett
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
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
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
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
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
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
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.

~~~
dualogy
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.

~~~
fleitz
Interesting, I noticed npm install stylus, does this mean Node only? Or is
there a ruby/rails version?

