Friday, June 6, 2014

A little Coffee for the morning

Opening:

I was challenged by a co-worker to write a website in CoffeeScript (CS) on a Node.js server after telling him my opinion on CS. He said he had the same opinion too but once he started to use CS he really liked it and suggested I give CS a try. So I decided to make a simple webpage using Node.js and CS.

My initial thoughts:

·         Why would anyone want to learn something that only adds an extra step to the development process? You have to compile CS to Javascript (JS) so it will run on the browser.
·         Before you can write CS you have to know JS. As they say “It’s just JavaScript”. Why learn two things?
·         As you write CS you have to make sure you are not breaking anything in CS and/or JS. Yeah if you mess up in CS when debugging life can get very interesting.

What is CS:

Okay what is CS? “It’s just JavaScript”. It is a language that compiles into JS.

Its goal is to make writing JS easier. It does this by taking away some of the oddities that you have to remember when coding proper JS. Here are two examples of this:
·         In JS you have to make sure to use three equal signs instead of two so you compare type as well.
if( 1 === 1) is not the same as if( 1 == “1”)
·         In JS you have to making sure to declare variables with ‘var’ so they are put into the correct scope.
var chicken = “bird” vs chicken = “bird”
It also saves you lines of code since it is like Ruby/Python meaning it is white space delimited and not bracket delimited.
while true
                var chicken = “food”
is the equivalent as the following in JS
                                while( true ){
                                                var chicken = “food”
                                }

The meat:

As I said I was going to make a website using Node.js and CS. Well I did not have any ideas on what kind of website to make so I decided to follow a tutorial I had a while back with Node.js, Express, and Jade. It was from http://goo.gl/lfwbVV. As I re-walked through the tutorial I converted the JS to CS. This process was not as bad as I thought it would be once I figured out the syntax for CS.

I started the converting process with the Node.js server file went from 34 lines of code to 28 lines of code. This is a pretty good save since it is just the base starter server file. The more you type the more you will save. The space saving is because CS is a white space delimited language. It was very nice to not have to remember to use === or var. That alone may be the saving grace of CS and why it has a strong following.

One thing I did find out, you need to convert any client side CS to JS so the browser can run the code. I had two options to do this. One was to use the IDE’s (PyCharm) built in ‘watcher’ functionality or to use CS’s built in ‘watcher’ functionality for this. I went with the IDE’s because I just had to click a button and not type a command (call me lazy). When you add a watcher to a CS file after every save it makes/updates two files. One file is the JS file that the browser/server will run. The other file is a map file this allows browsers like Chrome to be able to allow you to debug the CS code and not the JS code in them. That’s a point for Chrome ^5.

If you don’t want to add watchers to the server side CS file with node you need to add require("coffee-script").register(); to your Node.js server file. The debugger for the IDE I use has a hard time figuring out where the program is when running without the converted CS. So I ended up adding the watchers to the server side CS files anyway.

On the down side in my opinion is that it feels A LOT like Ruby to me and not so much like Python. What can I say Ruby on Rails left a bad taste in my mouth. The other down side is that you have to make sure your watchers are setup so you can debug your code. Those are the only two down sides I can think of if you are doing CS with Node.js. 

In the end of my little learning experience I came to the conclusion CS is great in the Node.js world but a pain in the non Node.js world.

In the end pro and con List:

Pro:
1.       CS allows you to focus on the coding part and not the structuring part of JS.
2.       You are using a white space delimited language. Meaning no braces, brackets, and more. Thus saving you lines and lines of code.
3.       Chrome Dev Tools can debug CS on the client side assuming you set up watchers.
4.       IDE’s can debug CS on the server side. Some require that you add watchers.

Things I don’t care for:
1.       It feels too much like Ruby (not a Ruby fan). Give me Python.
2.       It does add an extra level of complexity for debugging

The END:

I will still use CS for all my Node.js projects moving forward. If you still are on the fence you should give CS a try. Just go ahead and do a simple project with it. Closing thought is MEH it’s nice and all but it was not life changing or life destroying.

No comments:

Post a Comment