How CoffeeScript made me a better JS Developer

Roman Kuba
4 min readOct 8, 2015

--

Today, I love CoffeeScript. Love it for it’s simplicity, tidiness, easiness and especially all the power of JavaScript it allows you to use in a very easy way.
But that has not always been the case.

The early days

First I heard about CoffeeScript when it got traction. It was late 2010 I think. Highly motivated and still in university I thought it would be a great time to dive into this new script language.

I went ahead and:
Ordered a book about CoffeeScript;
Opened the book;
Started reading;
Closed the book;
Never looked at it again;

I was totally put off by CoffeeScript from what I saw in the book. All the examples were mathematical and abstracted in a way it was hard for me to draw connections to my day-to-day work as web developer.
From now looking back I knew the greatest problem was my poor understanding of JavaScript. I was pretty good at using it, making the best out of jQuery, yielding little scripts into the projects I made to reach results.

About 2 years later I jumped onto the Angular train and luckily grasped most patterns very quickly. Was able to build a super modularized application that did quite some magic. Must have been a stroke of genius when I wrote it.
In this process I got a deeper understanding of JavaScript and got more and more interested in writing better code.
Code that should be reliable, testable, extendable and foremost comprehensible when reading it months later.

Enter CoffeeScript … again

When I started at Codeship I was confronted again with CoffeeScript. The whole application is written in “Ruby on Rails”, so CoffeeScript is the obvious JS choice for most ruby developers.

I spent a few days diving into the already existing code. Analyzing classes, structures and functions. It was all very different from what I was used to write with Angular or other projects.

The first problem I needed to solve was creating a class that would allow us to parse terminal output from the server into style-able HTML. Previously I would have done it like:

// Simplified Code Examplevar stringToParse = '...'
var parsed;
function parser(raw) {
... do some magic parsing
return '...'
}
parsed += parser(stringToParse)

Yeah, that’s how you learn to do stuff but actually It never was really sustainable to build code that way. It was a ugly mess of variables and string concatenation once you would add some ajax to it and make it stream content. And what would happen if I would need to parse different terminal outputs and instances and what so ever. It will be messy, messy, messy!

Well let’s see how I did it in CoffeeScript:

# I use this instead of @ to not tag people hereclass Parser  constructor: ->
this.parsed = ''
parseCode: (raw) ->
... do some magic parsing
this.parsed += '...'
# Outside the class I would call it like sop = new Parser
p.parseCode('...')
console.log p.parsed # Will return parsed output

This version was so much more powerful. Create extra instances over and over and still keep the codebase clean. That’s how it should be.

When looking at the generated JavaScript code I saw all the stuff that it would require a developer to write. `Prototypes` are very powerful but super messy to write so I would have never touched those. Seeing CoffeeScript doing all the heavy lifting for me made me very excited.

Solving Higher Problems

Now after writing CoffeeScript regularly for 18 months I am even more excited than I was in the beginning. I can look at code I wrote a long time ago and mostly understand it. I would write some things better now than I did in the beginning but I am pretty happy with the codebase.

I started moving Angular controllers into CoffeeScript and and and. It also led me to the point where I think about different problems. How can I make my code better reusable, better testable, more performant. When I now need to touch pure JS code I write better code than I ever did before CoffeeScript because I look at it differently.

Conclusion

As some people now say CoffeeScript is dead, I think it’s as useable as ever. I am planning to post some more articles about CoffeeScript specific code solutions.
In general I just want to motivate you to give CoffeeScript another try if you were put off the first time like I have been. I now even use it in teaching for my students as I think it’s easier to make them better FrontEnd developers because they learn to write better code.

Consider following me on Twitter and here on Medium. I swear I’ll behave.

Your’s sincerely, — Roman

--

--

Roman Kuba

Code, Design and Web experienced by a part-time-nerd. Working @Codeship as Frontend Developer. Follow me on Twitter @Codebryo