33 comments

[ 2.8 ms ] story [ 46.6 ms ] thread
Every so often I'll try to switch to two spaces, but I fail. Four spaces magically fits the length of `var `, and allows for nice variable alignment which is too good to pass:

    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.
Even if I'm using two-space indentation, I will still use as many spaces as necessary for alignment or to make my code more aesthetically pleasing.
Technically, your second code example is using 2 spaces for indentation ;)
I use tabs for indentation, and I don't particularly care that I'm "out of touch with the JavaScript community." That strikes me as a fairly arrogant thing for the article to say, but regardless, two spaces doesn't work for me. It's just not enough indentation for my eyes.

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.

These are all good points.

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?

> 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?

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.

There is huge programmer bigotry against writing code in a readable font. I do not understand why.
If by readable you mean proportional, then I believe a lot of the resistance comes from vertical alignment issues. For instance:

  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.

There's a way to eliminate vertical alignment issues and make your code equally readable in a proportional or monospaced font: don't use vertical alignment.

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.

I resisted the switch for a long while, but once I did switch, I rapidly grew used to it, and I don't think I'd ever go back.

A lot of it really is just "what you're used to".

I suspect that two spaces make sense when you also limit the length of line to some maximum below 80.

I.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?

Yup, I'll be looking at that next. Do you keep your line lengths below 80?
Yes. Actually I limit myself to 79 columns! Not sure why, maybe to escape the idea that 80 is a punch card inherited limit... I am thinking to switch to 72, that's the RFCs limit.
> I.e. I suspect that those using 4 or tabs also use 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.

IIf you are trying to keep your line bellow the 80 cols by tweaking the indent size, my guess is that you have too much code in one line, which is fine, but can make looking back at the code a little bit more painful.
(troll ahead, forgive me I cannot hold myself for this one ;) ) Indenting javascript is like putting lipstick on a pig no ?
(comment deleted)
I use what is essentially Smart Tabs¹ (tabs for indentation, spaces for alignment) in JavaScript, and pure spaces in LiveScript/Coco, which complain when you mix.

¹ http://www.emacswiki.org/SmartTabs

Do you run into issues when collaborating on code? I imagine that the majority of people (either in favor of pure spaces or tabs) have trouble adapting to using both -- I know I would.
This seems silly, to me the only benefit of using tabs is so that users can view it with their own default tab size. In which case space-aligned lines would look out-of-place and probably be hard to read
No, because space alignment only happens within blocks of common indent level. It works, try it.
Semantic spacing (indent with tabs and align with spaces) is the standard in Go, enforcrd by gofmt. The compiler doesn't distinguish whitespace, though.
Douglas Crockford doesn't really care about spaces or tabs as long as the code is clean and legible. He said this in one of his talks, back when he was with Yahoo! Personally, I would prefer the 2 space indent.
The link between server side code indent style and javascript is quite interesting, as I unfortunately tend to indent my code all in the same way (tabs that take 4 spaces in display).
(comment deleted)
I really, really don't understand why tabs should not be used for indentations.

People argue that with tabs, you can't get a consistent style between computers, but that's kind of the point isn't it? It gives you a certain flexibility. For smaller screens you can set a tab to represent 2 spaces; in larger screens, 8 spaces. OTOH if the person uses 8 spaces to represent an indent, then there's not much you can do to change that. And having to tap 2,4 or 8 spaces every time just doesn't make sense to me.

(I know you can set the editor to expand tabs to 4 spaces, but if you're going to do that, why not just stick with tabs?)

Am I missing something here?

I think it depends on what language someone has come from before using JS. For instance if you come from a ruby background you're probably used to using two spaces.
Indent however you want to, and don't try to argue objectively for one way or the other. It doesn't work like that.

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.

Indenting with spaces is like carving your own girlfriend out of a block of pine. And insisting that everybody else make out with her.
While on the subject, what tools do you use to support your JS styling ? Linter / checker / autoindenter ...

I know of http://jslint.com/ and http://www.jshint.com/, any other one ? What is you're usual configuration for those tools ?

> Indentation is a matter of personal preference (it’s why they call it style, after all), but if you’re starting an open source project and want it to be consistent with community standards, indent with two spaces.

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