Thursday, April 19, 2012

Javascript Gentleman Rule #1

A Gentleman never goes past 80 characters

A simple one, and one I used to break myself a lot. It's cool writing bit long lines which do everything, after all you're writing less lines of code right?

Then I found that at my new work place my boss liked to check the commits in an 80 character width vim instance. I know we all have wide screens now but think about it for a second, if we split long lines up then all that whitespace to the right of short lines are just wasted space. Splitting a line at 80 columns means we waste less screen space, are compatible with other people's viewing habits and can also give us a rough guideline as to how much information we should be putting on to each line. Possibly the biggest thing we gain from this though is that because we now know the absolute width of each file we can have several editor windows (or a spit view) on a screen and know exactly how wide that window should be without having to scroll to the right.

Luckily most editors have an in built option to put a line at the 80 character mark, or wrap the text at that point to allow you to easily see when you've gone past it. Vim hasn't got anything directly in, but just do a quick google search and there are solutions to highlight the column or highlight text going past it.

All the major lint tools should also have options, and good old Closure Linter definitely does.

This does raise a question though - what should we do if a line is too long? how do we split it up? Hopefully you can deal with that in style guides but a good first rule is to make a new line at the last comma. I'll go in to indentation in another rule 

No comments:

Post a Comment