2 Spaces or 1 Tab, what's the standard for indentation in the Rails Community?

28,784

Solution 1

2 spaces is generally agreed-upon.

Solution 2

As for all the arguments about different editors showing tabs with different widths...

It is a configuration setting people!

Change your editors to use two spaces for each tab. Different widths in different editors is not an argument. It is a moot point. If anything, using tabs allows people who prefer different widths to set and use the width they prefer. Hell, I think I like 3 spaces.

And you have to hit backspace twice just to un-indent something once. I'm a programmer, I'm lazy. I don't like to do things more than once!

Solution 3

2 spaces. Even for the plain Ruby kids.

Solution 4

Two spaces are emerging as the standard because they are “just enough” indentation to make the HTML clearly indented to most people's eyes, but because HTML tends to nest very deeply — much more deeply than one would commonly nest when programming — and anything more than two spaces tends to start pushing HTML off the right edge of an 80-column screen pretty quickly.

Do not use tabs; use two spaces. Tabs are problematic because they can be a different width in different editors (and terminals, for that matter). Since whitespace is removed when you tidy your HTML before deployment, there is no ultimate bandwidth benefit to using tabs; they simply introduce a wildcard into your development experience.

I use Emacs, which auto-indents XML, HTML, and SGML with two spaces. I would be quite annoyed if I had to type indentation characters by myself; that's why I have an editor. :)

Solution 5

2 spaces is the convention for Ruby which Ruby on Rails is based upon.

Share:
28,784
Lance
Author by

Lance

I read JavaScript and am attempting to emit Assembly from it.

Updated on March 19, 2020

Comments

  • Lance
    Lance over 4 years

    I've noticed that most of the HTML/XML/HAML that gets generated from plugins uses 2 spaces instead of 1 tab. I use textmate and have tabs set to 4 spaces for HAML/HTML/XML and 2 spaces for Javascript/Ruby, but I only have to press the tab key once to get nice indentation. Pressing the space bar twice and delete twice seems like too much work :p.

    Do you manually type two spaces, or is some middle layer converting tabs to two spaces? Or do just a few of you use tabs?

  • Lance
    Lance about 14 years
    is there any reason behind this, for reference? thanks for the answer, I'll start doing that :)
  • Ben
    Ben about 14 years
    Well, since you asked specifically about Rails, I guess I can point to the fact that the Rails codebase itself uses 2 spaces. Since it is such a widely-used and contributed-to codebase, I think we can accept 2 spaces as convention.
  • Gerry
    Gerry almost 12 years
    So to answer your original question. It's standard because it's convention. And it's convention because it's standard. en.wikipedia.org/wiki/Circular_reasoning
  • Ben
    Ben almost 12 years
    I know @Gerry is just being funny, but no---it's just standard because it's convention. Those who originated the convention were not following a standard.
  • Gerry
    Gerry almost 12 years
    He wanted to know the reasoning behind the choice and your response amounted to "because it was chosen". I was poking fun at that.
  • konung
    konung almost 12 years
    This article talks about indenting on your screen - I'm talking about sharing code and having the best consistency, you can't expect other people to adjust their tab settings everytime they open your code.
  • Gerry
    Gerry almost 12 years
    I believe you might be confusing indentation level with alignment. Here is a deeper explanation: movementarian.org/docs/whytabs
  • konung
    konung over 11 years
    No I don't - in a sense that a lot of editors rely on indentation in code, to align it visually on the screen ( which also depends on font used - some kind of monospaced font is best usually - but that's personal preference).
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com over 10 years
    With tabs you are forced to configure the width to view properly. With spaces you are forced to configure width to edit with ease (e.g. vim on backspace removes multiple spaces). Therefore for spaces there is at least some chance that you won't have to configure only to view properly. Spaces take up more... space though.
  • Fernando
    Fernando over 10 years
    You can configure tab width once in your editor and never have to change it again. That's only if you don't want the default. Which on every modern IDE/editor I've used, defaults to 4.
  • user2854544
    user2854544 about 10 years
    The fact that we have to choose is stupid. We should give people the power to decide between two spaces or four spaces. Oh ! Well... it's called Tabs...
  • Jens
    Jens over 9 years
    People always say they prefer spaces because code looks the same everywhere, as if that was desirable to everyone. The idea of using tabs is that people can see the code the way they please. If your code looks bad when someone changes tab size then it's your fault, not the editor's. Code should look good regardless of my choice of tab size for my editor/viewer (given that I choose a sane size, say between 2 and 8). People should stop forcing everyone else to use their preference of indentation, and that's what tabs give you.
  • Fernando
    Fernando over 9 years
    I would like this chiseled into my tombstone. Well said, Jens.