Lee Quarella
Lee Quarella

Reputation: 4732

Rails coding standards - Why 2 space indentation?

In reading about rails coding standards, it seems clear that 2 spaces is generally accepted as the way to do things. Why has this gained traction? Is it just the most widely used practice and therefor best to use for consistency, or is there another reason it is actually better than tabs or a different number of spaces?

Upvotes: 31

Views: 19671

Answers (3)

Brian Glick
Brian Glick

Reputation: 2201

It's a matter of convention. The really important thing is consistency.

Most (but not all) developers prefer spaces to tabs because they look the same regardless of any particular text editor / ide setting. http://www.ecyrd.com/JSPWiki/wiki/WhyTabsAreEvil

Two spaces over four is also a matter of convention. Ruby code aims to minimize extra characters, and I suppose extra whitespace goes against this trend.

Upvotes: 35

bwv549
bwv549

Reputation: 5859

  1. Because ruby has built in support for anonymous blocks, lots of ruby code ends up being nested more than in other languages. 2 space indents allow for more nesting in a given width.
  2. Spaces always look the same in every editor (consistent look and feel)
  3. Convention

Upvotes: 24

Ray Toal
Ray Toal

Reputation: 88378

It comes from Ruby. There is an "unofficial" Ruby style guide here:

http://www.caliban.org/ruby/rubyguide.shtml#indentation

There is no real reason as to why two spaces beats eight or four. Perhaps it is because Ruby code often has shorter lines than Java and C, which tend to use four?

Upvotes: 9

Related Questions