CoffeeScript and 5 Things You Need To Know About It In 2021

Ever heard the saying “putting lipstick on a pig” ? Well, in this case, JavaScript is the pig, and Ruby is the lipstick – with maybe a little bit of Python blush colouring its cheeks, and a touch of Haskell mascara.

Although initially visually jarring, how the pig looks in lipstick does tend to grow on you after a while until you prefer it to the original. The same can be said about CoffeeScript – all the familiarity, look and feel of Ruby, the syntax of Python and Haskell – while still compiling to JavaScript.

A prettied-up pig has never looked so good.

Barnyard animals and women’s cosmetic products aside, CoffeeScript is actually something you should be looking to at least gain a base competency in by the end of this year.  It’s a simple, clean, fast programming language that compiles straight to JavaScript – whether via a caching framework plug in on the server at build time, or later on during runtime on the browser itself.

The truth is that JavaScript has some really good parts, they’re just occluded by awkward code – waiting to burst out and show the world all they can be. CoffeeScript is the broom to sweep away the trash.

JavaScript has a bit of reputation of throwing up issues so frustrating that it’s enough to illicit a cringe response from any seasoned programmer. It helps to think of it this way though – CoffeeScript is just JavaScript, except faster, and better in almost every way. Time to get on the bandwagon.

1 – WHAT IS COFFEESCRIPT

Basically, CoffeeScript was made by people who hate JavaScript syntax, intended to be used by other people who hate JavaScript syntax. It’s compatible with all flavors of JavaScript, and adds in “syntactic sugar” from other programming languages in the same way you sweeten up your morning cup of Joe – hence the name.

It’s been around for roughly a decade, and exists as a Node.js utility as well as an Apache Maven plugin. The official CoffeeScript website even has a feature to try it out for yourself, opening up a modal window so users can see how CoffeeScript is entered, and the subsequent JavaScript it outputs – run directly through the browser.

2 – WHAT COFFEESCRIPT IS USED FOR

CoffeeScript has a peculiar side effect – it helps the naysayers and non-believers better understand how good JavaScript can be and how it works at a root level, while at the same time teaching them to write more optimized code.

Basically, you write all your code in CoffeeScript, then run it through the compiler to be outputted as JavaScript that can be chucked straight into the browser – minus the headache of writing in JavaScript.

CoffeeScript can be used  anywhere you’d use regular JavaScript, and it can also use all of its libraries too – think interactive websites, game development, that sort of thing.

3 – WHY COFFEESCRIPT IS POPULAR

So why would a JavaScript programmer want to use CoffeeScript? Well for starters, it lets you get the job done with significantly less code – after all, we all love efficiency.

It also comes packed with little bits and pieces added on like Ruby’s string interpolation, Python’s style list comprehension, and Haskell’s binding variables for example. It’s these quality-of-life additions that make even common tasks significantly more tolerable in CoffeeScript when compared to the original JavaScript.

4 – THE PROS:

SWEETEN ME UP

Of course, the big upside here is the syntactic sugar added to CoffeeScript to make it much easier to express your programming while using significantly less code. Dustings of Ruby, Python, and Haskell make life so much easier.

EMBRACE THE GOOD

Using CoffeeScript actually encourages people to use the good JavaScript patterns that can sometimes be muddled by writing in the original code. The ability of the CoffeeScript compiler to pump out straight JavaScript actually promotes its use.

REJECT THE BAD

Conversely, because CoffeeScript makes JavaScript easier for those with a little bit of background in other programming languages, it stamps out the anti-JavaScript sentiment that seems to be getting more and more toxic every year.

MAKE JAVASCRIPT GREAT AGAIN

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.

UNVARIED VARIABLES

The variables in CoffeeScript are auto-scoped, meaning you can’t really overwrite globals by forgetting to type “var” in, shadowing a variable with the same name, or have variables interacting in separate files.

CALL ME MAYBE

The omission of the word function and it’s associated curly wurley parentheses, in lieu of a simple “->” arrow, makes call-backs a hell of a lot easier to do. Semantic indentation makes it clear when call-backs are nested, and likewise, that little arrow helps to preserve the “this” when it’s appropriate to do so.

USE LESS BRAIN CELLS

As an English speaker, it’s way easier to use “unless x” instead of getting your head around the clunky syntax of “if (!x)”, as well as “if x?” being easier to parse than “if (x = != null)”. Brain cells are precious, so allocating them to working the logic instead of working out the logic of the syntax seems like a better use of your cranial resources.

5 – THE CONS

TOO SWEET

Odds are if you hate Ruby, Python, or Haskell – you’ll hate CoffeeScript as well. A programmer will generally be more productive in a language they actually like programming in – which may seem like a no brainer, but it’s something that’s often overlooked.

PAINFUL COMPILATION

So, it may seem a bit counterintuitive after singing the praises of the CoffeeScript compiler, but the syntax errors it can spit out are vague to say the least. Looking at the JavaScript output itself is also no help, as the bug comes from the CoffeeScript input code itself. There isn’t really any way to marry up the compiled JavaScript lines of code with the original CoffeeScript.

COMPATIBILITY NIGHTMARE

So, with every new version of CoffeeScript that comes out, so too does the code change slightly. What you wrote 6 months ago may actually now compile differently on a new version of CoffeeScript. While this is the same in Ruby, Python, and other programming languages, generally with original JavaScript the code runs just fine across multiple versions of multiple browsers regardless of the versions.

PARLE VU COFFEESCRIPT

Think of JavaScript as English, a language that has been widely adopted across most of the globe as either a native language, or a commonly learned second language. Though CoffeeScript has become pretty popular, it will likely never have the same widespread community that JavaScript has – making it something resembling Icelandic, for example.

If JavaScript is causing you headaches but you still need to use it for your programming role, why not give CoffeeScript a try? Those with backgrounds in other programming languages are at a distinct advantage.

That being said, CoffeeScript is very polarizing. Those who love it, really love it – while those that hate it, really, really hate it. It really does boil down to a team decision in the work setting. If everyone is onboard, productivity will skyrocket due to the sheer volume of code you won’t be writing and also how clean the resultant JavaScript will be.

But what about junior programmers, who barely have a grasp of JavaScript, Ruby or Python to begin with? The debugging cycle can be a bit of a nightmare for even a seasoned veteran, and you’ll find that you sink a hell of a lot of time into fixing up less experienced team member’s work instead of your own – killing productivity.

HOW TO LEARN COFFEESCRIPT

In our opinion, these are the 5 best online resources to learn CoffeesScript – a full range from absolute beginner to grisly programming veteran. Hey, most of them are even free – music for the ears if you’re a cheapskate!

 1 – THE ORIGINAL DOCUMENTATION

As it turns out, the documentation on the CoffeeScript official website is fantastic. It’s all inclusive, and explains the syntax and capabilities of the programming language extremely well – worth looking at first, definitely.

2 – THE LITTLE BOOK ON COFFEESCRIPT

After sinking your teeth into the basics, why not take a look at The Little Book On CoffeeScript to start looking at more examples of what can be done using the language.

3 – COFFEESCRIPT COOKBOOK

Intermediate to advanced programmers would do well to keep the CoffeeScript Cookbook open in their browser. It gets into the nitty gritty of the language, and has some awesome cheat sheets to act as a quick referral while coding.

4 – COFFEESCRIPT TUTORIALS

If you’re more of a visual learner, take a look at this 12-part series of videos that go through how to use CoffeeScript as an easy to follow, step by step process.

5 – COFFEESCRIPT RISTRETTO

Expert level programmers may want a more in depth look into the nuances of the CoffeeScript programming language, and may do well to skip the beginner and intermediate resources and head straight for the heavy stuff.

Next Steps

We hope this article helped you learn more about Coffeescript and why you should give it a try!

If you enjoyed this article you may also enjoy Rustlang vs. C++ Which Language Should You Learn in 2021 or  P5JS and 5 Things You Need To Know About It.

Kofi Group offers a multitude of resources to help you learn more, improve your career, and help startups hire the best talent. If you are interested in learning more get in touch today!