Monday, July 14, 2014

Rules on javascript style - Part 1

So style and standards are very important to me. I'm a bit of a fanatic when it comes to them, reason is that they make for better code that is easier to maintain and easier to get new developers up to speed on.

With the javascript community there are a lot of standards out there and some teams have none, others say they do but they make little sense to people. And when you ask why? you get multiple answers which is a sure sign you have unclear standards.

So to start out with I'm going to pull in an except from one of my favorite standards guide, written by one of mine and your favorite companies, Google!

This is the section on naming:
use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS,, and filenameslikethis.js.
 The filenames is the one I see the most common errors on, and people will say it will never happen but I've worked in mixed teams before, and by mixed I mean windows, mac, and ubuntu, and we had issues.

Filenames should be all lowercase in order to avoid confusion on case-sensitive platforms.
Filenames should end in .js, and should contain no punctuation except for - or _ (prefer - to _).

The last part I myself have sort of broken but I'm not sure it really is a break. For unit tests I will do filename.spec.js as the format I feel it makes it easier to spot the test files.

Anyhow more later. Enjoy!