Skip to content

Instantly share code, notes, and snippets.

@Jun-Dai
Last active August 29, 2015 14:02
Show Gist options
  • Save Jun-Dai/0d1f9c6a2fc6cd362f4a to your computer and use it in GitHub Desktop.
Save Jun-Dai/0d1f9c6a2fc6cd362f4a to your computer and use it in GitHub Desktop.
Tabs vs. spaces

An attempt to summarize all the arguments around tabs vs. spaces.

Tabs:

  • Each developer can pick their tab size - if one dev likes 4-column tabs and the other likes 2-column tabs, they are both happy.
    • counter: really?
      • counter to counter: yes
  • Fewer characters in the code. This matters more for things that go over the wire (HTML, XML).
    • counter: if you really care, you'll minify anyways. This is pretty trivial.
  • It is easier to type and erase when using a tool that doesn't insert spaces when you press the tab key or delete them when you hit backspace.
    • counter: when are you writing non-throwaway code with such a tool?

Spaces:

  • A lot of utilities will display tabs as 8 columns, which pushes code far to the right, and that can be awkward. You don't always have control over these utilities' settings or want to waste time changing them (e.g., troubleshooting something on a server). Examples: github, opengrok, Notepad, vi, pico, <pre> tags in browsers.
  • Viewing in a tab-size other than the one the code was written in will disrupt any vertical alignment made in the code: http://jarrodoverson.com/blog/spaces-vs-tabs/
    • counter: many (most?) coding standards discourage this sort of alignment anyways.
    • counter: if you specify the tab-width in your coding styleguide, you can keep the alignment when using tabs
      • counter to counter: But then you lose a key benefit of tabs, and it will suck in any tool that doesn't understand your coding styleguide (e.g., GitHub, Notepad).
  • You can't have a coding convention that has different levels of indentation for different contexts unless you use lots of tabs:
    • counter: why would you do this?
public static void main(String args[]) {
    LOGGER.info("This is a string that needs to be brokenfor readability, but our standards " +
          "say to indent by 6 spaces, halfway between 1 and 2 tabs in a 4-space world.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment