Last Updated: September 09, 2019
·
5.567K
· javier_toledo

Coffeescript, is worth to learn it!

A few days ago a coworker asked me if was really worth to learn CoffeeScript as at first sight he didn't noticed too much differences from JavaScript. This was my 'spontaneous' email response and I think I convinced him :-)

-With CoffeeScript you'll write more compact and readable code, with less punctuation signs and more plain english words. For example, you'll find yourself using 'and' instead of && or 'unless condition' instead of 'if(!condition){}'.

-You can use the # operator inside strings to compose dynamically generated strings. This is far more readable than a random mix of plus signs, code and quotes and if you build some HTML dinamically you'll find how useful it is:

"<a href='#{calculate_url()}'>Click here</a>"
instead of
"<a href='" + calculate_url() + "'>Click here</a>"

-CoffeeScript is a real object oriented language. While you can emulate OO with JavaScript using constructors or the prototype property, CoffeeScript has its own specific syntax to define classes, methods, constructors or instance variables. Real OO eases to build a good architecture and organize your code.

-The => operator. I love this one as it allows you to define a function which will be executed on current scope without explicitly using a closure. In example:

//JavaScript
var Thing = function(){
    this.importantData = 42;
    this.doSomething = function(){
        var closureVar = this; //We save a reference to this object
        window.setTimer(1000, function(){
            //This method will be executed from window scope as it's triggered by setTimer
            //We use the previous saved reference to access the correct value
            alert("Response to life, universe and everything is: " + closureVar.importantData);
        }
    }
}

# CoffeeScript
class Thing
    constructor: ->
        @importantData = 42
    hacerAlgo: ->
        window.setTimer 1000, => # This operator makes the closure for us implicitly, that's great!
            alert "Response to life, universe and everything is: #{@importantData}"

-In CoffeeScript you can easily iterate over array elements

//JavaScript
var whatever = [4,3,2,1];
var doubles = []
for (var i = 0; i<whatever.length;i++) {
    doubles.push(whatever[i]*2);
}

# CoffeeScript
whatever = [4,3,2,1]
doubles = []
doubles.push(number*2) for number in whatever

-Ability to write conditions after code makes it also more readable:

doSomething() if emergency
doSomething() unless nothingHappens    # Even on iterators to filter some of original array items
phones = ['iPhone', 'Nokia 3230', 'Samsung Galaxy III']
alert "#{phone} is awesome" for phone in phones when phone isnt 'Samsung Galaxy III'

Most of CoffeeScript's improvements makes your code more readable, and more readable code is easier and cheaper to maintain code. This is specially useful when you work on a non-trivial JavaScript project or you need to share code with other people or with yourself in a few months.

That are only some of advantages of this awesome language, so just go to CoffeeScript website and give it a chance:
http://coffeescript.org

12 Responses
Add your response

For me, the most exciting advantage is The Existential Operator.

http://coffeescript.org/#operators

over 1 year ago ·

I tried convincing my colleagues with https://speakerdeck.com/jeroenr/coffeescript

I believe I also succeeded :)

over 1 year ago ·

I love CoffeeScript, but is is not a real OOP language. (It can't be when it is essentially just JS) It fakes OOP, and it does a desent job, but you will realize the limitations when you start doing heavy extending.

One example: Lets say B extends A, and adds property b. Now A has a method a that uses property b. Well unless b is a function that returns a value, B won't be able to use it inside a. (Because extending in CS is not extending in the true OO sense) Try it.

But never the less, I do much prefer writing CS to JS.

over 1 year ago ·

@jisaacks if I have understood well your example, if B extends A adding the property b, then A cannot have a method a which uses b as b is only present in B, so if B overrides a with its own version of a which uses b, it will work as follows: A will have a method a which doesn't use b and B will have a method a, that overrides the a from A, which uses the property b as expected on any OO language :-)

I'd say that CoffeeScript is more than JavaScript just in the same way that C is more than assembler or Ruby is more than C, it's a matter of abstraction, and OO is an abstraction. It doesn't really matters if CS is on shoulders of JS or a Turing machine if it works as a great abstraction that let coders work faster and better.

Thanks for your comment!

over 1 year ago ·

@javier_toledo you missed understood what I was talking about. I will explain the real world example. I was creating a base Backbone.View class that defined a render method, the render method called a template method that the base class did not define but expected descendent classes to define. A descendant class would extend this base class and just define what template to use, but the inheritance would not work correctly unless I enclosed the template function inside another function.

As for your second paragraph, you could not be more far from the truth. The difference between CoffeeScript and all those other languages is that there is nothing that executes CoffeeScript code, CS gets converted into JS before executing. So it IS JavaScript when it runs. Those other languages like Ruby are true languages, yes Ruby was written in C but it is a real language with an interpreter, it does not get converted into C and then run in the C interpreter. As long as CS is converted into JS before it is executed it can never be more or do more. Now it can be an awesome abstraction that makes writing JS fun, concise, and fast, however.

over 1 year ago ·

And JavaScript IS an object-oriented language. Arguing that it's not just because it doesn't have a 'class keyword' is silly.

http://en.wikipedia.org/wiki/JavaScript <- Paradigm - object oriented.

Prototypes are a very a powerful concept. If you understand them and read Design Patterns again you can easily see that those patterns are just some special cases for prototypes.

over 1 year ago ·

@khasinski I would say JavaScript is a paradigm-agnostic language. It's all up to you how to use it.

over 1 year ago ·

I see you haven't clicked the link so I'll paste it here: Multi-paradigm: scripting, object-oriented (prototype-based), imperative, functional. It fully supports EVERY aspect of OO programming. Classes are also just a special case for prototypes.

over 1 year ago ·

@jisaacks May be you want to define the template function as an empty function in your base class so the descendants can extend it and work as expected, on other languages it could be named an abstract method :-)

I'm still convinced that CoffeeScript is an OO language, your argument about that it's just JS and therefore not OO capable is very weird taking into account that you can actually write a cross compiler from C++ (via LLVM) or Ruby into JS without loosing their OO capabilities:

https://github.com/kripken/emscripten/wiki

http://opalrb.org

And even CoffeeScript can actually be directly interpreted on your shell via the coffee command over node and even be interpreted by Ruby without an intermediate JS conversion thanks to poetics gem (https://github.com/brixen/poetics)

Compilers and Interpreters are a weird field of investigation and fun, but remember that at the end of every language there is a simple processor that runs a very simple set of instructions without any kind of object related abstraction.

over 1 year ago ·

CS is fully OO, JS is fully OO. How much more convincing do you need? ;) If you need class keyword to program OO then you do not understand OO enough
.

over 1 year ago ·

@khasinski JS is not a language designed specifically for OO, but it can actually behave as any OO language in a very easy way (https://coderwall.com/p/z6-u_q?i=6), so at least is a Object-Oriented-Capable programming language XD

over 1 year ago ·

-> ClojureScript.

over 1 year ago ·