Pinterest Stumbleupon Whatsapp
Ads by Google

I’ve never really liked writing JavaScript What is JavaScript and How Does It Work? [Technology Explained] What is JavaScript and How Does It Work? [Technology Explained] Read More all that much. From the day I wrote my first line using it, I’ve always resented that whatever I write in it always ends up looking like a Jackson Pollock painting. Blegh.

coffeescript2

Yep. Despite its power and flexibility, JavaScript is by no means a pretty language. It takes verbosity to an extreme and has so many quirks and idiosyncrasies, even the most famous book about the JavaScript programming language nods to its inherent nastiness with its title ‘JavaScript: The Good Parts’.

coffeescript3

A Better JavaScript

It doesn’t have to be this way though. JavaScript has a wealth of amazing tools and libraries, and if you use CoffeeScript, you can make brilliant websites and tools without dealing with syntax which makes your corneas bleed. It’s also CoffeeScript’s time to shine, as more and more beginners look to JavaScript for their first language due to its usage in client and back end web development, as well as much of HTML5.

Aesthetically, CoffeeScript looks and feels like Python or Ruby. The 5 Best Websites To Learn Python Programming The 5 Best Websites To Learn Python Programming Over the past decade, the Python programming language has exploded in popularity amongst programmers in all areas of coding. From web developers to video game designers to in-house tool creators, many people have fallen in... Read More It’s genuinely, astonishingly beautiful, and adopts certain language conventions that make it easy to learn. It really does feel more like a Rembrandt than a Pollock. And yet, it compiles down to JavaScript, allowing you to use it everywhere you use it, including front end development and node.js. You can even use it with jQuery jQuery Tutorial - Getting Started: Basics & Selectors jQuery Tutorial - Getting Started: Basics & Selectors Last week, I talked about how important jQuery is to any modern web developer and why it's awesome. This week, I think it's time we got our hands dirty with some code and learnt how... Read More .

Ads by Google

Getting a hold of CoffeeScript is easy. There are a number of ways in which you can install it. Firstly, if you’re using a recent version of Ubuntu Linux, you can install it using the built-in package manager. Experience, however tells me that it is likely that the version in your repositories is an older version.

As a result, it’s probably a good idea to grab it using the Node Package Manager. The Node Package Manager (npm) is a little bit like apt-get or Brew, but is only really used for getting Javascript packages and libraries. Handily, NPM comes with node.js and is available for OS X, Windows and all flavors of Linux.

coffeescript4

To install it using NPM, run the following command as root:

$ npm install -g coffee-script

The ‘-g’ trigger installs it globally, allowing you to use CoffeeScript anywhere on your computer, and to invoke it from the command line wherever you are. You’re now ready for a great deal of CoffeeScript fun.

“It’s Just JavaScript”

It is important to remember that the golden rule of CoffeeScript is “It’s just JavaScript”. This is both an advantage but also a disadvantage. If you have had the chance to watch Gary Bernhardt’s hilarious talk, ‘Wat’, you’ll know that JavaScript has its share of warts and weirdness. For the most part, CoffeeScript shares these. After all, it is essentially an abstraction of JavaScript.

However, this isn’t entirely a bad thing. Firstly, It allows you to use CoffeeScript with your Node, Angular, Express or Backbone projects. In addition to this, you also get to use Google’s V8 JavaScript engine, which is a very fast interpreter for Javascript. Whatever you make, you can be assured of it being face-meltingly fast.

I must confess that I might be a little bit biased. Having had the opportunity to spent the past few months writing a great deal of CoffeeScript in my day job, I can attest to how lovely it. Indeed, given my prior exposure to it, I’d heartily encourage novice and journeymen developers to look into as a starting language.

Perhaps one of the most compelling reasons why I am so eager to espouse the virtues of CoffeeScript is because I feel that it has adopted some incredibly innovative language design choices that mandate good software development practices. This means that whatever code you write, odds are good that it will be pretty readable.

In particular, CoffeeScript mandates that you indent all of your code in a sensible, logical manner. Whatever code you produce, it has to follow a logical hierarchy. If it fails to do so, the CoffeeScript compiler yells at you and refuses to execute your code. This will start to feel all too familiar if you have ever written any code in Python.

coffeescript5

I’ve observed that CoffeeScript was clearly created with an aim of being really expressive. Syntactically, it’s remarkably similar to plain English, albeit with a few extra parentheses and arrows carefully scattered around the place. An advantage to this is that it becomes trivial to verbally express the flow of your program. Handy if you’re a novice programmer trying to get feedback on your code from someone much more experienced, or working for a software development company that extensively makes use pair programming, such as the one I do.

Further adding to its novice-friendly credentials, in the short period in which it has been in existence, CoffeeScript has managed to develop a rather active and friendly community. Stuck beginners are more than welcome to approach them with questions, and for the most part they’re usually happy to respond. In addition to a strong presence on Stack Overflow, an official website that contains a CoffeeScript interpreter within the page itself and a great many publications about it, you’d be hard pressed to find yourself stuck for too long.

A Beautiful Language

Greg Pollack, the founder of Code School, defines a beautiful programming language as one that allows the developer to get the most stuff done whilst using a minimal amount of code. By this definition, CoffeeScript is a beautiful programming language. From the ground up, it feels just remarkably well designed.

Perhaps one of the most sensible choices it makes is abandoning JavaScript’s insane prototype-based way of doing object orientation. Instead  it is replaced with a far more elegant system that is based upon classes, which will be familiar to anyone who has dabbled in some of the most popular OOP based languages out there . This drastically lowers the barrier to entry for many programmers. It also does this whilst not entirely jettisoning its functional credentials.

The language itself feels a little bit less weighty, too. Huge swathes of CoffeeScripts bulk is excised. Variables aren’t declared using ‘var’. Parentheses and curly braces are almost as rare as a Bengal tiger. You don’t even need parentheses to pass in parameters to a function. Every function returns something, giving you one less thing to worry about. Mind blowing stuff.

Despite its obvious youth, CoffeeScript has gotten a great deal of attention, and for very good reasons. CoffeeScript was recently included by default in Ruby on Rails, the web framework that powers sites such as Github and Groupon. It’s not just companies which are flocking to it in droves, as developers have started to embrace it and have created a wealth of CoffeeScript plugins, including for Sublime Text 2 Try Out Sublime Text 2 For Your Cross-Platform Code Editing Needs Try Out Sublime Text 2 For Your Cross-Platform Code Editing Needs Sublime Text 2 is a cross-platform code editor I only recently heard about, and I have to say I'm really impressed despite the beta label. You can download the full app without paying a penny... Read More and Vim. This means that you don’t have to go without syntax highlighting and code-completion to use the latest, shiniest hipster web technology.

Conclusion

CoffeeScript is a breath of fresh air, and is a welcome addition to any developers toolkit. However, there are little niggles which detract from the experience of developing in it which one hopes will be fixed in later iterations of the product. .

Firstly, despite error handling being wonderfully explicit about the problem you’ve encountered, its not always too obvious where the problem is. Then you are told of the  line which has the erroneous code, it is rarely in the CoffeeScript code you’ve created, but rather is in the compiled JavaScript. This means that you have to go through the irritating process of compiling your code to JavaScript in order to debug it. Incredibly unhelpful.

I have since learned that if you compile your code and then pipe it to ‘nl -ba‘, the outputted results are significantly easier to read and contain sensible line numbers.

Another annoyance I personally experienced was when reading documentation is written in Javascript, you have to mentally translate it to CoffeeScript. Whilst this is initially a major cause of frustration and annoyance, I found that this subsists when you start to get used to the language. It’s just a matter of persistence.

CoffeeScript reinvents the wheel. There’s no doubt about that. However, it reinvents the wheel, and then turns it into a rhinestoned, bedazzled, sparkly wheel. CoffeeScript is a beautiful wheel indeed. Will you be moving to CoffeeScript for your day-to-day development? Let me know in the comments.

  1. Dewi Morgan
    July 30, 2013 at 6:54 pm

    The downside of being just JavaScript is that... it's just Javascript.

    HaXe is not just Javascript, but also Flash, C++, and C#, and PHP, and Java, and Neko, and Tamarin, and Unity3D, and...

    If you're using a metalanguage, why use one with just a single compile target?

    Of course, it depends on your task: if you're only ever writing websites, it kinda makes sense: it only needs JS, and your code reuse will be limited to other websites, which also only need to be JS. Still... it just feels weird to use a metalanguage for a single language.

    If you're writing apps or games, then odds are you'll someday need to target a non-web platform. At that point, if you have locked yourself into using a non-portable JS-only language, then you will need to port every line of it by hand. But if you picked a language in the first place that had a variety of compile targets, then porting for free, and as a free bonus, you get code reuse between projects that use different platforms.

    In practice it's not as easy as just selecting a new compile target (recompiling Furc for HTML5 would require a LOT of work to hack around the absence of socket comms in JS), but it's a heck of a lot easier than rewriting from scratch.

    The big win, though, comes in terms of maintenance: rather than having to maintain a separate code base for every compile target and platform, you just maintain one: a bugfix or feature-add in your code will affect all targets, without needing to be ported.

    • Matthew H
      July 31, 2013 at 10:31 am

      Interesting thoughts! I've actually never used HaXe. Any good? Reckon it's worth an article?

      One thing I forgot to talk about in the article. The compiled code is JS best practices code. You're actually producing code that's good. From a metalanguage, that's huge.

Leave a Reply

Your email address will not be published. Required fields are marked *