Skip to content

Instantly share code, notes, and snippets.

@hzhou
Last active May 29, 2016 17:46
Show Gist options
  • Save hzhou/a99506767b5c8b6bcb9c64edbe5c3e03 to your computer and use it in GitHub Desktop.
Save hzhou/a99506767b5c8b6bcb9c64edbe5c3e03 to your computer and use it in GitHub Desktop.
Indentation: Tab or Spaces

Code Indentation: Tab or Spaces?

When this question is raised, we often expect to choose from either using tab or spaces, and either answer has merit and in the end neither can be decisively chosen. As such, a general purpose editor often have options to allow both. However, in practice it often implicitly offers a third option -- a mixture of tabs and spaces. For example, vim explicitly list this as a feature -- it will intelligently replace spaces with tabs as we indent.

The purpose of this note is to make it clear that although either choice of tab or spaces as indent have merit, the mixture of tabs and spaces definitely has no merit at all!

So when we come to editor tab settings, we should really only deal with two types of options. One vs. the other, instead of one and the other (and more). The current situation in vim look like:

:set tabstop=4 softtabstop=4 shiftwidth=4 expandtab 

That is 4 separate options and the combinations of them creates way more than just 2 choices!

So I suggest to narrow down (and prevent those defenitely bad choices to be ever made):

  • If we do not set shiftwidth (default 0), then the indent uses tab (1 tab for each level of indentation), and we may set tabwidth for actual display, and expandtab should not be available (noexpandtab).
  • If we set shiftwidth, then expandtab and tabwidth should not be available. By default expandtab is always set and tabwidth remain default (8).

This should be adopted by all editors unless there is legitmate third choices that a sane user may prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment