published on in JavaScript
tags: coffeescript javascript

Why I Use CoffeeScript

Yeah this is a reply post to Oscar Godson’s “Why I Don’t Use CoffeeScript”. I don’t have any problems with people who don’t like CoffeeScript or other similar solutions but I don’t like if someone cuts in the air with fake reasons and self made generalities.

Syntax Sugar

A lot of developer said to me “A real language has curly brackets” and similar sentences about Ruby or Python but when I ask about Lisp or Assembly then I get a simple “eeeee that is completely different”. But why? Why can’t you imagine a language without C syntax?

No it’s not only a Sugar. Why I have to write

var cube;
cube = function(x) { return square(x) * x; };

if I can write a simpler format like this:

cube = (x) -> square(x) * x

Ok. With this example this is really a Sugar but when I want to write more complex code, I get a lot of cool stuff like:

obj?.func?()

Oh yeah if I’m not lazy OR I have more time I can test obj has that func property and the func property is a function and if a function then call it without parameters.

my_string = "#{S4()}#{S4()}-#{S4()}-#{S4()}-#{S4()}#{S4()}#{S4()}"

I think it’s more readable than writing it with concatenation (CoffeeScript compiles the string as concatenation) or using string.format() where I can miscount the parameters.

And these are only highlight of the iceberg… Read the CofeeScript book.

(Not to mention that you can learn a lot from the generated JavaScript code.)

Wart Removal

CoffeeScript doesn’t fix “warts” of JavaScript like for ... in ..., because it isn’t a “wart”. CoffeeScript gives us a “tool” that makes work easier and we don’t have to write helper function for it. Without var life is easier but it isn’t an ”oh-i-love-god“ category (it’s not a problem so we don’t have to solve it) and if you leave var in JavaScript you make a mistake (yeah it’s a mistake).

CoffeeScript is not an alternative to learn JavaScript it’s a language/tool for programmers who know what is JavaScript. If someone ask me about JavaScript, CoffeeScript and learning then I tell him/her that he/she first has to learn JavaScript. If you can’t write good JavaScript code then you can’t write good CoffeeScript code.

Classes

JavaScript is a f*cking prototypal language but we can us it as Event-Object oriented language. Why is it better to write an own extend function or use an external library with plus kilobytes and even more code to parse (like Backbone.js). Is this really a Simple JavaScript Inheritance? If you want to use this why don’t use that:

class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved #{meters}m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45

sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"

sam.move()
tom.move()

oh yeah… Compile this code:

var Animal, Horse, Snake, sam, tom,
  __hasProp = Object.prototype.hasOwnProperty,
  __extends = function(child, parent) {
    for (var key in parent) {
      if (__hasProp.call(parent, key)) child[key] = parent[key];
    }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };

Animal = (function() {
  function Animal(name) {
    this.name = name;
  }

  Animal.prototype.move = function(meters) {
    return alert(this.name + (" moved " + meters + "m."));
  };

  return Animal;
})();

Snake = (function(_super) {
  __extends(Snake, _super);

  function Snake() {
    Snake.__super__.constructor.apply(this, arguments);
  }

  Snake.prototype.move = function() {
    alert("Slithering...");
    return Snake.__super__.move.call(this, 5);
  };

  return Snake;
})(Animal);

Horse = (function(_super) {
  __extends(Horse, _super);

  function Horse() {
    Horse.__super__.constructor.apply(this, arguments);
  }

  Horse.prototype.move = function() {
    alert("Galloping...");
    return Horse.__super__.move.call(this, 45);
  };

  return Horse;
})(Animal);

sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");

sam.move();
tom.move();

Yeah, really… Much better. =P

Conclusion

I understand JavaScript is not a classical language and I know we need a different think about structures and mechanism. I love JavaScript. Really. Sometimes I meet a problem and I need to sit back to my ball, draw graphs, lines and equations but after that, I can write a quick solution. I can’t imagine my life without JavaScript inside a browser and that is the reason why I don’t think if someone like CoffeeScript then this programmer hates JavaScript. And don’t forget CoffeeScript has a big codebase on developer’s side but the client gets only a small JavaScript instead of loading and parsing an extra library like underscore.jsjQuery and others. So if you don’t need a big part of the loaded library you have a lot of unused code. A build process (compile CoffeeScript into JavaScript) is painless instead of loading a relatively big extra JavaScript file for 3-4 functions.

P.S. If someone hate JavaScript then it has a big chance he/she will not loving CoffeeScript too and wouldn’t be a front-end developer.