Hacker News new | ask | show | jobs
by dstein 5417 days ago
The problem with Coffeescript is if you already know JavaScript, it's completely unreadable. Looking at code like this, I have absolutely no idea what it does:

  class ItemView extends Backbone.View
    render: ->
      @options.items.each(@renderItem)
    renderItem: (item) =>
      @el.append item.get("title")
It doesn't make sense because there are magic variables appearing out of nowhere - @options, @el, append -- what are these? I'd have to study the resulting JavaScript to understand what it's really going to do.
4 comments

I'm afraid you're getting tripped up by the Backbone.js library, not the language itself...

`this.options` is the options object that was passed when the View was created, and `this.el` is the default DOM element that wraps the View. For more info, see:

http://documentcloud.github.com/backbone/#View-constructor

http://documentcloud.github.com/backbone/#View-el

This is the major problem with CoffeeScript: it seems so straightforward, there must be something wrong with it.
I'd have to study the resulting JavaScript to understand what it's really going to do.

Once.

And then you could use shorter, more convenient syntax for the rest of your life. Worth it?

The same could be said of any language you are unfamiliar with. Ignoring the inheritance, which should be familiar to anyone with class-based OO experience, that code basically translates to:

    function ItemView() {
      this.render = function() {
        this.options.items.each(this.renderItem.bind(this));
      }

      this.renderItem = function(item) {
        this.el.append(item.get("title"))
      }
    }
In this example, I don't see any significant difference as far as readability goes, to be honest. I will note, in case it is not entirely clear, that the use of Backbone in the example is unrelated to CoffeeScript.
Except you left out setting the prototype chain to extend from the super class. CoffeeScript expresses it succinctly. The example above, while readable, already has much more noise than signal.
How is that not the case for any language?

(This also might be a bad example, but it was based off of code I wrote)

Coffeescript is adding an abstraction level, but all the complexity of JavaScript remains, so while you write Coffeescript, you have to sort of compile it in your head. I could maybe get on board if it went one whole level of abstraction further.
I disagree; I think the more standard handling of `this` and the automagic variable scoping, constitute a significant reduction in complexity for people coming from other OO languages. Certainly for me, having more experience with Python and Ruby than Javascript.

The abridged syntax is just gravy.

Just as a data point, most "on top of" languages seem a bit more distant from what they sit on top of.

In other words, C is somewhat cross platform, and much higher level than assembler. Ruby does way more out of the box than C does.

Coffeescript seems to mostly just be a modified JavaScript syntax, although I have to admit I haven't looked at it that closely.

That's all C++ originally was, too. Everything has to start somewhere, and fixing some of JavaScript's warts is as good a place to start as anywhere.