SHARE:
Uncategorized

Sugar in Your Coffee

Flatiron School / 2 July 2013

The following is a guest post by Mendel Kramer and originally appeared on his blog. Mendel is currently a student at The Flatiron School. You can follow him on Twitter here.

JavaScript hurtin’ your Ruby-biased mind? Maybe a little coffee ‘n sugar is all you need!

When it comes to improving upon JavaScript, we have plenty of choices. Here I look at two of them: CoffeeScript and SugarJS.

“Learning JavaScript used to mean you weren’t a serious software developer. Today, not learning JavaScript means the same thing.” – Tim O’Reilly

These days, JavaScript is no longer an asset for a web-dev; it’s an absolute requirement. Unfortunately, it has been said that JavaScript has bad parts. That has been said to be an understatement.

Some History

FWIW, this is not a judgment on Brendan Eich, who created JavaScript in 10 days back in May 1995. It was rushed out, rough edges and all, to preempt Microsoft’s attempt at an even worse proprietary language of their own, during the era of the Browser Wars, which in turn became the quintessential proof to “Always be Shipping!”

As JavaScripts community grew to include devs from other languages and disciplines, solutions sprang up to alert the presence of, side-step, and, in the case of CoffeScript, even solve these bad parts.

While there is some overlap in these solutions (particularly while trying to “shim” ECMA support in legacy browsers such as IE8), generally speaking they take different direction.

CoffeeScript

CoffeeScript is a language created by Jeremy Ashkenas (creator of the BackboneJS project among many others) which compiles directly to JavaScript. This means, you can’t serve .coffee files directly to the browser, rather it must first be compiled to a .js file first.

You can learn all about it on its homepage, where there’s a tool to run CoffeeScript in real-time. I’ll demonstrate just one example here. The basic tenets should be familiar to Rubyists:

  • Parens are optional
  • Semi-colons are banished
  • Implicit return statements
  • Lots of syntactic sugar


…compiles to:

SugarJS

SugarJS in it’s own words

… is a Javascript library that extends native objects with helpful methods. It is designed to be intuitive, unobtrusive, and let you do more with less code.

It has lots in common with Ruby, and even has a comparison page to see the syntactical differences between it and Ruby. So:

All Together Now

Because the fiat currency of the browser is JavaScript, as long as whichever shortcuts you use produce valid JavaScript, you can write code that intertwines all these examples.

While there’s obviously an improvement in these example over the plain JS, if you’re having trouble understanding any of the plain JS code, you’re doing it wrong. Debugging will be ever more difficult if you don’t understand the “magic” in your code. (Anyone who has struggled with Rails and its particular form of magic can attest to this.) If you plan on writing client-heavy apps, there’s is no escaping learning JavaScript thoroughly, warts and all.

Conclusion

Being currently a student of a dynamic language (Ruby) and the ever-growing popularity of this family of languages (i.e. Perl, Python, PHP etc), I may never get to write any non-trivial apps in a language that need to get compiled. As CoffeeScript may be the closest I get, and to satisfy the requirement of at least one XKCD reference, I leave you with this:

303: ‘Are you stealing those LCDs?’ ‘Yeah, but I’m doing it while my code compiles.’


All Revisions to this document are visible here

Previous Post The Nested Ruby Params Hash for Complex HTML Forms and Sinatra Next Post