Skip to content

Instantly share code, notes, and snippets.

@saibotsivad
Last active December 1, 2016 15:13
Show Gist options
  • Save saibotsivad/06021a81865226cfc140 to your computer and use it in GitHub Desktop.
Save saibotsivad/06021a81865226cfc140 to your computer and use it in GitHub Desktop.
The right way to indent

The "correct" way to indent

The issue of tabs-vs-spaces is a long and revered Flame War, so I don't think I'll be changing any minds with what I can write on the topic.

However, it's what I use in all the code that I personally write, and any projects I make public use it.

When to tab

When you want to indent.

When to space

When you want to align.

When to tab and space

On the same line, first tabs, then spaces.

Examples

(Spaces are shown as · and tabs as in these examples.)

When indenting a function, tab:

function doThings() {
↦var someNumber = 123
}

When aligning text, space:

/**
·*·==== the =====
·*····aligned
·*······text
*/
function someBigFunction() {
}

When aligning indented text, tab then space:

function getTheZipCode(input) {
↦//··········city,····80924···(-?1234)?
↦var regex = '\w+,\s+(\d{5}(?:-?\d{4})?)'
}

Doing it wrong

Once you have a space on a line, no tabs are allowed after it.

This is wrong:

/**
·*·Column1↦Column2
·*·val1a↦val1b
·*·val2a↦val2b
*/

And this is right:

/**
·*·Column1······Column2
·*·val1a········val1b
·*·val2a········val2b
*/

And this is just the worst:

function getTheZipCode(input) {
····//↦↦↦↦city,····80924···(-?1234)?
····var regex = '\w+,\s+(\d{5}(?:-?\d{4})?)'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment