CoffeeScript Misconceptions
You may have already heard about CoffeeScript and some of the hype surrounding it but you still have found several reasons to not make the switch. This blog post is for you. Here are some of the reasons I held out for so long:
- I wanted to understand Javascript and just didn't see how using a "simpler version" (my own thoughts) would make my life easier in the long run.
- If I DID use an intermediate language, I wanted to be able to dump it at any time and not feel like I was forced to continue using it.
- Putting one more thing with bugs in between myself and my code seemed fool hardy.
So here's the reasons I finally switched:
- It's less verbose Javascript, not a different or simplified language.
- A couple of shortcuts that enable you to use list comprehensions rather than error prone for statements.
- CoffeeScript compiles to pretty awesome Javascript. I wouldn't have any concern dumping CoffeeScript at any time because of this. It would also have put some great conventions in my Javascript that I could follow.
- Eli Thompson is always right. (You should read his blog. He's smart: http://eli.eliandlyndi.com/)
For-Loop Boiler Plate Banished
Here's an example of several Javascript for-loops embedded in a switch statement:
GNARLY! Now obviously we could clean this code up a bit... but seriously...
Well let's just see that same code in CoffeeScript and how much better it can be:
That's the power of expressions.
Less Complex OO
Now how about classes? These are the bane of Javascript programmers everywhere. There are few right ways to do them and a billion wrong ways. CoffeeScript classes are the biggest simplification CoffeeScript makes to Javascript and was a big reason for my holding out for so long. Really though, they're just short hand that removes a bunch of boiler plate so I have less opportunity to introduce bugs. Here's a simple example:
Simple Javascript class:
Same class in CoffeeScript:
Get Off My Lawn
In the past, people tried compiling to Javascript simply because they didn't get it. This is different. It's been over a decade since Javascript began to see wide use and enough of us get it now that we're starting to see tools that don't try to cover it up for being a broken language. Instead, we're seeing improvements made to a programming language we love and think can be even better. The only reason I see remaining to stick with Plain Old Javascript is nostalgia and fear of change.
I'll leave you with one last example. It's a full set of CoffeeScript that does drag/drop and zoom/pan in canvas. You can decide for yourself which version of the code you'd rather work on.
Before CoffeeScript:
After CoffeeScript: