

Do JS styleguides reflect how popular open source code is indented? - atroche
http://atroche.org/post/30994290348/javascript-indentation

======
Stratoscope
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.

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

~~~
Stratoscope
> 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.

------
ricardobeat
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.

~~~
jhrobert

      var one = 1
      var two = 2

------
arrowgunz
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.

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

~~~
atroche
Yup, I'll be looking at that next. Do you keep your line lengths below 80?

~~~
jhrobert
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.

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

~~~
sil3ntmac
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

~~~
Evbn
No, because space alignment only happens within blocks of common indent level.
It works, try it.

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

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

------
jrajav
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.

------
dvhh
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).

------
netghost
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.

------
dickbasedregex
Indenting with spaces is like carving your own girlfriend out of a block of
pine. And insisting that everybody else make out with her.

------
gbin
(troll ahead, forgive me I cannot hold myself for this one ;) ) Indenting
javascript is like putting lipstick on a pig no ?

~~~
Achshar
No.

