Posted by & filed under Coding.

I’ve spent the last couple of days switching to Sublime Text from TextMate. Which is a solid move. Thank you very much for the heads up on that one Winsha. And in addition to that I’m teaching myself CoffeeScript.

I looked at CoffeeScript when it first came standard in Rails and I wasn’t impressed. It felt to me like it was a language for people who hated Javascript and wanted a different syntax. But I gave it short shrift and I realize that now. James Polanco convinced me to have a second look and I’m really glad I did. It’s clear to me now that it was written for folks who love JS, are fluent in JS, understand JS best practices and who want to make those best practices easier to… well… put into practice.

As an example, this:

$divs = $('div'); for( var o in $divs ) { $divs[o]... }

Is going to be faster than:

$('div').each( function() { this... } );

Because you aren’t invoking a function on each iteration, and having to create a new context and all of that. In my little ad-hoc test the for loop was about ten times faster. Another nice advantage to the ‘for’ loop is that the this reference remains the same so you don’t have to do little tricks like ‘bind’ or setting a ‘self’ variable to reference that context in closure. I like the ‘.each’ style because it’s clean and looks like Ruby. But with CoffeeScript I get the ‘for’ back because it handles all of the grunt work for me.

Honestly, it’s not clear to me that CoffeeScript would even be around if the value of ‘o’ in the for loop example were the object and not the index. It’s those little decisions that make all the difference.

It’s not just iteration though. There is a lot more to like. The explicit class stuff is great. The visual de-cluttering allows you to really see what the code is doing. The significant white space is definitely growing on me. The ‘fat rocket’ syntax for maintaining the context in the function is very clean. And the fact that almost everything that can be an expression is. That in particular I like a lot. For example.

chords_i_like = [] for c in chords chords_i_like.push( c ) if c.good()

Becomes:

chords_i_like = ( for c in chords when c is good() )

The ‘for’ loop becomes a ‘select’ operation in Ruby/Rails parlance. Which is very, very cool.

Plus there are a nice fixup around hash key naming where you can just put { text, title, comments } into a hash and that translates to { text: text, title: title, comments:comments }. But the first version looks cleaner and encourages engineers to use better variable naming.

I’m glad to say I was wrong about CoffeeScript. It’s an excellent addition to the Javascript language. Or whatever it is. Honestly, I’m hesitant to say that I “learned a new language” since it’s really just short hand for writing good JS.

Also, a big shout out to the Peepcode intro to CoffeeScript. It was a bit labored and the guys vocal style was grating after a little while. But the content was excellent.

Leave a Reply

  • (will not be published)