Starting with CoffeeScript

So the time has come for me to look into something interesting and not work related. I should have done this long time ago, stupid me. At work (yea, I lied saying it won’t be work related) recently I’m coding in system specific scripting language. Or should I say “scripting” language. Basically it is subset of C with some built-in functionalities for financial systems. I’m pretty new to this but I’ve already noticed that coding some small scripts in C is not the most pleasant experience. Nor it is efficient. Creating forward declaration of functions, declaring variables at the beginning of block, remembering all different types and managing memory – it’s boring and time consuming when all i want to do is calculate some things, check few conditions and maybe send an e-mail. This got me thinking – what scripting language would be better for such task? And that’s how I realized I don’t really know any such language at all.
Of course I’ve done few things in JavaScript but it was nothing really fancy, just small snippets using jQuery to make pages more fancy and maybe do some Ajaxing. Nothing more.
I’ve heard about Perl, Ruby and probably few other such languages, but I’ve never coded single line of code in them. This made me angry on myself – why didn’t I? It’s always good to know how something can be done in different language, get a fresh look and maybe touch a new paradigm. And I was wasting such great opportunity to learn new stuff. Not for long, I said to myself.

First I tried Ruby since it was getting a lot of buzz last few years (and even Jeff Atwood uses it – Why Ruby?). So I started reading, trying some things out (it even proven useful as quick and simple way to solve small problem in everyday life). But I would need to learn about Rails platform and other stuff to build something for the web – which I think is simplest way to share your work with the world. Sure, I could learn it – and will in future – but now I didn’t feel like it. So I remembered that there was something like CoffeeScript – language that compiles into JavaScript. Given I know a little of JS and want to build for web – it was decided.

To start with CoffeeScript one needs first to install it. I’ve picked the easiest way – first install NodeJS and then, using simple command install CoffeeScript:
npm install -g coffee-scirpt
This way we will have compiler available globally – everything exactly as coffeescript.org states. Easy way to verify if everything is OK is to write some code! But why bother with files for easy checks. CoffeScript compiler allows us to pass code as a command line parameter, so lets use this feature.

With a quick look into help (coffee -h) we see that passed parameters say to generate output code to console, pick a code from command line parameter and for easier reading – do not generate top-level function over the generated code (this function normally serves as outer block limiting variables lifetime and helping avoiding overwriting variables from other modules).
Exact code meaning is pretty simple – display alert for each number in array – syntax is simple and easy to understand. Generated code also seems pretty neat. That’s exactly what I was looking for – nice code compiled to nice output that can be easily used on the web.

So we can code, but we also need to make sure that the code works. We can of course check manually if the JavaScript works using any modern browser, but that’s no fun. Unit tests are fun – so we will have a look on how to write some of those for CoffeeScript. I’ve decided to use Jasmine testing framework, which also supports writing tests in CoffeeScripts. We will have a look how it works in next blog.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s