Do JS styleguides reflect how popular open source code is indented?
atroche.org
atroche.org
In particular, I like to code in a proportional font. I find this more pleasant for reading code, just as I prefer proportional fonts for other text. Two spaces is definitely not enough in a proportional font. It's like a single space in a monospaced font.
But even when I switch to a monospaced font, I just don't enjoy reading code with two-space indentation.
I'm curious: if you were starting an open source project and you wanted to make contributing as seamless an experience as possible, would you still use tabs for indentation?
As for coding in a proportional font, I've never even heard of that before. I'm tempted to try it just for a day, to see if I can get over the initial revulsion. Which font do you use?
That's an interesting question, because reducing friction for contributors is important. But yes, I'd still use tabs. It honestly sounds like the least of my worries.
Clearly, many people prefer two-space indents as your study shows. But how many people are seriously inconvenienced if they have to edit a file using tabs? Don't modern programmer's editors detect a file's indentation settings automatically? Or do they? I'd hate to use an editor that didn't. I use Komodo IDE and even though I have tabs as my default, when I load a file using space indentation it uses the correct setting for that file.
> As for coding in a proportional font, I've never even heard of that before. I'm tempted to try it just for a day, to see if I can get over the initial revulsion. Which font do you use?
Well, if you think you'll find it revolting, then maybe you will. :-) It depends a lot on your coding style. If you use column alignment (alignment after the indent, as opposed to simple indentation) much, a proportional font won't work for you. I stopped using alignment in my code a long time ago, so when I tried a proportional font I hardly noticed any difference.
I've tried a number of fonts, but the one I keep coming back to for some odd reason is Georgia - I just like the way it looks on my ThinPad display. Georgia is kind of a radically proportional font - numbers are proportionally spaced along with letters, where many other proportional fonts have monospaced numbers - but I just find it pleasing to my eye.
Komodo has a killer feature for proportional font programming: when you set up a font/color theme, you specify two fonts, a proportional font and a monospaced font, and you can switch between those two fonts easily. Oddly enough, they don't provide a keyboard shortcut by default, but it's easy to set one up in their keyboard shortcut dialog - I use Alt+O on Windows. So when I'm reading code from someone who uses column alignment, I can just hit Alt+O and read it in a monospaced font.
def func_with_lots_of_parms(parm1, parm2,
parm3, parm4):
In my experience that's pretty difficult to do consistently with a proportional font, and then when someone changes to a different font, the alignment will be off.To me, formatting code is more about creating a good experience for other people (and future me) and less about pleasing present me.
About ten years ago, I got tired of having to adjust the spacing whenever I edited the function name in code using vertical alignment. I'd been running across too many cases where people would take code like this:
def func_with_lots_of_parms(parm1, parm2,
parm3, parm4):
and change it to: def betterFunctionName(parm1, parm2,
parm3, parm4):
Oops.So I asked myself, who do we have a different rule for indenting with parentheses than we do for braces? We don't indent functions like this (JavaScript example):
function foobar(param1) {doSomething();
doSomethingElse(param1);
andWrapItUp();
oneMoreThing();}
Geez, you'd have to re-indent the whole thing if you came up with a better name for foobar()!So I tried indenting function parameter declarations and function calls just like function bodies, and I found I liked it quite well:
def func_with_lots_of_parms(
parm1, parm2,
parm3, parm4
):
doStuff()
or JavaScript: function doSeveralThings(
param1, // one interesting param
param2, // another interesting param
paramWithLongerName, // comment too, but don't "align" comments
lastParam // that's enough
) {
doOneThing();
finishUp();
}
Some time later I discovered that whatever version of Visual Studio I was using back then supported proportional fonts, so I tried it out and found my code looked pretty much the same either way, just nicer looking and more of it fitting on the screen with the proportional font.If I'd still been using a lot of column alignment, though, I probably wouldn't have liked the proportional fonts at all.
A lot of it really is just "what you're used to".
var one = 1,
two = 2;
// or
var one = 1
, two = 2
I also feel that when you have deeply nested callbacks, two spaces are not enough to visually separate indent levels at a glance. var one = 1
var two = 2I.e. I suspect that those using 4 or tabs also use long lines.
And long lines are not considered very readable these days...
As a result, my personal conclusion is that two spaces indented code shows a stronger interest in readability.
But I don't have numbers to back this claim and I understand it is contreversial. Maybe the author of this nice tool can add info about long lines?
Not really, I think length of indentation affects the length of the line only if there is a lot of nesting. For HTML I use 2 spaces as I know there is going to be a lot of nesting, whereas in JS, not so much, so 4 spaces works perfectly for me. Also, in JS, chained function calls be aligned on multiple lines which makes it more readable as well.
Also every project (regardless of whether you stick with the community standards) should use a .editorconfig file to define their code style. We made EditorConfig because it's 2012 and indentation wars shouldn't still be happening: http://EditorConfig.org
I know of http://jslint.com/ and http://www.jshint.com/, any other one ? What is you're usual configuration for those tools ?
Rather than argue the merits of spaces versus tabs, it's much more important to make sure that you have a consistent style within a single project, especially with multiple developers.