Tuesday, December 29th, 2009

CoffeeScript: A nice little language that compiles to JavaScript

Category: JavaScript, Library

<p>coffeescript

Jeremy Ashkenas is experimenting with a new language that translates down to JavaScript. The language is CoffeeScript and I kinda like the syntax.

Jeremy told us:

I’ve been working on a little language with a Ruby/Potion-esque syntax that compiles into JavaScript. It tries to enforce “the good parts”, convert statements into expressions automatically, and adds some extra goodies like array comprehensions and conditional assignment.

Think of it as JavaScript’s less ostentatious kid brother — the same genes, roughly the same height, but a different sense of style. Apart from a handful of bonus goodies, statements in CoffeeScript correspond one-to-one with their equivalent in JavaScript, it’s just another way of saying it.

The code looks like this:

coffeescriptcode

I particularly find interesting the JSON style elements (assigment, etc) and how well the code reads without function() all over the damn place. There are nice examples of easy reading in general… for example: launch() if ignition is on.

Nice work Jeremy!

Related Content:

Posted by Dion Almaer at 6:41 am
11 Comments

+++--
3 rating from 58 votes

11 Comments »

Comments feed TrackBack URI

I don’t like Java with its clogged syntax, but what you are suggesting looks unnatural to me. JavaScript has just right balance with that, and if you want, you can omit some parts of it and use “shortcuts”.
This one doesn’t introduce anything good, everything looks too similar and can lead to confusion. Better use C like syntax – widely used, with is another plus.

Comment by paziek — December 29, 2009

I’m not saying that I hate c-like syntax, but I’m all for the day when it is less of a success factor. I think that day is rapidly approaching, thankfully.
.
I agree with Dion, this looks pretty nice. Well done.

Comment by genericallyloud — December 29, 2009

The syntax reminds me of Rebol.

Comment by RonH — December 29, 2009

So the idea is to create a compiler in one language to compile a second language into a third language?

Why not just write Javascript?

Plus, the syntax seems like it would cause difficult to read code. For instance, declaring a variable and a function is way too similar. After hundreds of lines of code it might be hard to easily tell the difference. I like declaring variables with var and functions with function as it is obviously easy to tell the difference at a glance.

But it does look like a good project to learn how to create a compiler.

Comment by travisalmand — December 29, 2009

@Travisalmand,
Just 1 compile phase, CoffeeScript to JavaScript.
.
CoffeeScript is actually easy too read without much ambiguity, you can clearly see the type of a variable by simply looking at the assignment.
.
@Jeremy
I actually quite like it except for… How inheritance is resolved…
x.prototype = new Y();
Is baaaad. Ya need to move the prototype of Y to an empty function at the very least, otherwise inheritance will keep calling the constructor code… Which you don’t want most of the time.
.
CoffeeScript makes it easier and faster to write javascript, when you can cope with choosing an easier syntax over your beloved JS syntax, the extra compile step is something all JS programmers and “ninjas” simply have to get to grips with sooner or later.

Comment by BenGerrissen — December 29, 2009

Tom Robinson wrote a Narwhal module to integrate CoffeeScript in to Narwhal. This means you can get a REPL for CoffeeScript and use it in all of your other Common JS projects now.

His changes are merged in to the main repo now: http://github.com/jashkenas/coffee-script

If have narwhal installed, just run “tusk install coffee-script” and once that is finished you can open up a CoffeeScript REPL with “cs” on the command line.

Comment by fitzgen — December 29, 2009

@BenGerrissen :
I believe Travisalmand is referring to the fact that CoffeeScript is being compiled using Ruby to output JavaScript.

…the extra compile step is something all JS programmers and “ninjas” simply have to get to grips with sooner or later.

Why?

Comment by TNO — December 29, 2009

@TNO,
Because most JS developers/ninjas already are ;) (kinda). Compressing/minimizing for example is already a small part of compiling. The closure compiler goes a step further, removing redundant functions/code.
.
A lot of javascript developers also take great care on how their code looks and performs, this is all a blatant waste of time (guilty myself btw). Consider how much time you spent *tweaking* your code and realize a compiler does/can do that in a teensy fraction of that time. You gain more time to focus on actual functionality and add in proper unit testing etc.
.
Current JS compilers aren’t all that great though, there’s still much wheel to be reinvented/innovated for JS compilers and some issues to overcome like “modular on demand/async js”, “third party js” and how to fit it all in with optimizing HTTP requests.
.
In any case, if/when good time saving JS compilers that go beyond mere compression hit the streets, the game will change drastically for js developers, ergo:
…the extra compile step is something all JS programmers and “ninjas” simply have to get to grips with sooner or later.

Comment by BenGerrissen — December 30, 2009

@TNO, oh yeh, compilers are not just abominations that try to let you write one language in another language, but I do think you know that ;)

Comment by BenGerrissen — December 30, 2009

I’m wondering if there’s something that will be a higher-level abstraction than Google NaCl, but lower-level than Javascript as a compile target. Something like a bytecode for other languages to be implemented on top of, as Microsoft and Sun have done with .NET CLR and the JVM, respectively. I remember that Mozilla was doing something with Tamarin and they wanted to port IronPython/Ruby there somehow with bytecode.

Comment by antimatter15 — December 30, 2009

@antimatter, js is currently the only available interoperable standard compile target browserwise. Anything else would be too browser/environment specific and thats basically the jest of it.

Comment by BenGerrissen — December 30, 2009

Leave a comment

You must be logged in to post a comment.