Javascript Style Guide

Posted by in HTML5, Programming

javascript

javascript

I must confess that I needed a Javascript style guide, the same way I improve my Python skills after following a Python style guide (and you could  read about it here).

My progress with Python, thaks to the style guide

Before continue I want to talk about my experience in Python with the style guide, because following it has given me new skiils for writing clean code, think about how to write good code, and especially for the future, legible code. In this sense, it is vital the naming of variables, always in lowercase.

I guess in other languages there is also this style guide, but until I met Python, I had never been interested in this. Now, should I regret that, but …. better late than never !!

Javascript style guide

The Javascript style guide I’m following is the one published by AirBnB, a famous startup, available on their GitHub account: https://github.com/airbnb/javascript (you also have a spanish translation: https://github.com/paolocarrasco/javascript-style-guide). This is not he only guide, I’ve also met Google one, and even the jQuery one.

The guide is large, stating with the bassic (data types) moving to more complex topics such as events, modules, or jQuery. Reading it takes time, and putting into practice event more, but it worth it!

After reading the first content, it’s very attractive to me even knowking that I don’t like Javascript very much, because the guide is very pythonic, and lots of recommendations are similar to Python. About variables name convention, it follows the Pascal style (not Python).

Personally, this guide make me view Javascript with better mood, also because it forces developers to have a style defined for the common good. For those starting with Javascript, knowing the different patterns is a challenge because it is difficult to understand that the same thing can be written up to 4 or 5 different ways, and of course …. this does not help to understand the language. Neither does the fact of using Prototype (it seems to me it’s like something hidden, big words Extra complexity) to increase functionality.

You can also read the style guide for ES6, the new Javascript engine.

Things I miss in Javascript from Python

Regardless of the guide that you make reference, one of the things I like about Python is the way to create strings that must be completed with values. Thus, while in Javascript (and many other languages) are performed as follows, mixing characteris and variables:

var cadena = “A value is ” + a + ” while B is” + b

In Python, it’s very clear:

cadena = “A value is %s while B is %s” % (a, b)

or even:

cadena = “A value is {0} while B is {1}”.format(a, b)

With no doubt that the first big differencies between both languages is the use of ; instead of indentación on Python.

Guides are your friends

Although guidelines can be tedious, I think are very important for those developers who already have some experience and need to improve your programming style. This is my case, and I opted for the AirBnB as my reference reading (and I am not alone, as there are few other companies, some quite large, which have already previously selected this style guide!). The Google style guide is much more complete, and it’s perfect for a consultation of a particular topic.

As I progress with Javascript, I guess there will be more things that I missed from Python (hopefully I hope not the other way around!!), but what I‘m noticing is that my deep to Javascript hatred begins to subside, partly forced because Javascript has become one of the languages used today, even for me.

Happy coding!