31

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?

0

3 Answers 3

35

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.

0
23
  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
2
  • 2
    #2 is not specific to spaces. Consistency is consistency for either tabs or spaces.
    – Brenden
    May 12, 2016 at 17:48
  • 3
    @Brendan - I'm thinking of the case where one person may set their tab to equal 4 spaces in one text editor, and another person set their tab to equal 2 spaces in their text editor. A script with tabs in it will not look the same in the two text editors. Whereas a script with spaces will look the same across all text editors if it is the same.
    – bwv549
    May 14, 2016 at 12:29
9

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?

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.