Why I have given up on JavaScript

I think I’m not the only one who think that JavaScript is very ungentle in its syntax. Complex code is true challenge to maintain. Lots of braces, overlapping variables and control over the scope has already brought thousands of programmers to madness. Also understanding proper patterns of programming and prototype-based inheritance doesn’t come easily for most of people. But JavaScript is evolving, after years of stagnation, dimmed by Flash and its object and event driven paradigms.

 

The wind of change

As you probably know last years brought a lot of changes for JavaScript. Many open-source projects which were developed on Google Code and GitHub had broken almost monopolistic prime of jQuery-oriented plugins and a bit less popular MooTools framework. JavaScript has started to shaping it’s maturity by creating front-end frameworks and finally changing the point of view on MVC model, as a single inseparable unit. Currently, many start-up projects in Silicon Valley relays on MV* or MVVM frameworks. If that happens – it’s decided – that’s the future model of web-development. So more and more single-page websites appear basing on templates build on client side, containing hundreds of asynchronously download assets and moving parts of business logic to the client side. With effect in growth of JS code to thousands of lines. And at that moment of glory of JavaScript I gave it up. Why? Because I wanted to be ready for future changes.

coffescript logo - javascript development language

Say hi to CoffeeScript

I decided to gave up JavaScript for CoffeeScript – programmer-friendly language, created to bring relief to most of current JavaScript’s problems. His success can be proven by 11th position on GitHub’s language popularity list and sugar python-like syntax that makes code much cleaner and human-readable by making radical changes in syntax structure:

  • Objectivity ECMA 6 ready

    Unlike classic JavaScript that primarily emphases prototype inheritance, CoffeeScript focuses on it’s objective aspects. That makes it worth to learn, due to announced changes in EcmaScript 6 [currently draft] standard, that will add objective paradigms to JavaScript such as: objectivity, inheritance, constants or truly limited scope variables. These changes are very similar to what Coffescript currently offers, so learning it is preparing for new, that will surely come.

          class example extended by superClass
            # property
            @property
    
            constructor: ->
              # call function
              @method()
    
            # function
            method: (parameter) ->
              alert(parameter)
        

    If your looking for more information about ECMA6, Nicholas C. Zakas has shared his thoughts about new JavaScript syntax in his post on NCZOnline.

  • Brace-less code

    CoffeeScript let you optionally omit most of the braces in your code, making the code less enigmatic and almost brace-less. From my experience, applying all the brace-removal features makes more mess in code than not using them at all, but there is always good balance-point that works smoothly.

    Learn how to write readable and clean CoffeeScript code.

  • String interpolation

    With CoffeeScript you can forget about the concatenation of the strings that can be cleanly placed into the strings.

            name = 'John'
            alert("hello #{name}")
          
  • Managing the scope

    Due to fact that var keyword is forbidden in CoffeeScript, the control over the scope of the variables is gently hidden and prevents overriding. Explicitly, your whole code inside one file will be wrapped inside anonymous function to create inviolable namespace for JavaScript.

  • Type coercion in comparison operators

    Inexperienced programmes often forget about the difference between == and === operators. The effect is quirky code, that unexpectedly stop working, because the interpreter implicitly tries to convert the values before comparing of values. Example below shows it precisely:

            # true
            true == 1
            # true
            '2' == 2
    
            # false
            true === 1
            # false
            '2' === 2
          

    CoffeeScript prevent coercion errors happen by forcing usage of === in comparison operations. It lowers the code efficiency a bit, because == is faster than === in execution, but by forcing that, prevents substantial errors in the code.

  • @ shortcut

    To make writing this faster, really smart alias @ was added. It stands out of the code and it’s very easily cached by eye. For me it’s meaningless syntactic sugar, but yet very helpful.

  • Control over this handler

    The most gorgeous thing I love in CoffeeScript is definitely fat-arrow operator that allows to decide on handling scope of this handler when changing scope in code.

          class clickClass
            $(elem).on('click', @clicked)
    
            # This === clickClass
            clicked: (e) ->
              alert(@)
    
            # This === clicked element
            clicked: (e) =>
              alert(@)
          
  • Comprehensions

    Looping over arrays and hashes is very crucial in every language. The more we can enhance the experience, the better language is. CoffeeScript’s juice is made form that particular feature, allowing programmer to nicely and very efficiently iterate over the value+index of an array, or the key+value of a hash, hiding beneath nasty code.

            results = ( book.name for book in bookstore when book.rating > 3)[0...10]
          

    Learn more about CoffeeScript comprehension from post on OpenMind.

Criticism of CofffeScript

CoffeeScript is a language that constantly evolve and already has created huge society around. Yet it is still far from perfection. When you’ll google CoffeeScript you’ll probably meet with strong words of criticism about this language from other people, complaining about it’s non-JS, sugar syntax and lack of efficiency. Being honest makes me tell the truth. Some, if not most, of them are right in their complains. CoffeeScript is not language for you if you begin your journey with web-development. To take 100% efficient code from time to time you have to optimize it manually after compilation by CS, because the generated code is not always the fastest one. So, only if you fully understand the basics of JavaScript that lies behind this gentle language, you should consider learning it.

On the other hand I can say that CoffeeScript outputs heavily optimized code to the ground. It puts the quality of avarage JavaScript code to much higher level and allows to prevent very substantial errors. So, when I meet people who think that CoffeeScript is bad ‘just because’, I can only say that they are perfect example of Python Paradox – people being afraid of changes – and cut my discussion with them.

CoffeeScript is like putting lipstick on a pig, but it is damn good lipstick.

Final words

CoffeeScript is the very successful successor of JavaScript and creatof of the way JavaScript currently evolves. Moreover it has shown how strongly the society can influence the shape of the programming language. So, if you’re not newbie to JS, take deep breath and enjoy the world that the JavaScript will be within few years, after release of ECMAScript 6. CoffeeScript will allow you to write clean JavaScript, and delivers business value by helping you to write maintainable JavaScript faster that it otherwise would.

Further lecture - relative links to interesting articles

Further lecture

If you feel that you want to go deeper into CoffeeScript see the links below:

If you read it, you should share it: