DPH.AM

I like to draw, code, and build stuff.

Don’t Mix Spaces and Tabs

I personally love spaces, but I only prefer spaces over tabs because it’s not tabstop-dependent. There are editors (and code hosts such as github) out there using different tabstops and not intelligent enough to interpret tabs as different whitespace characters depending on the number of tabs/spaces used on the line right above it.

For that reason, what you see as…

1
2
3
4
/**
 * @string1      String      Hey I'm a string
 * @boolean1     Boolean     And I line up with the guy above me
 */

may look like..

1
2
3
4
/**
 * @string1                String              Hey I'm a string
 * @boolean1     Boolean      Uh oh, I don't line up =(
 */

There are worse examples, I promise. The point is my tabstop value may be different than yours and my editor may not be as intelliJent as yours, so try to play nice. When you’re working on a project with other people, using JUST spaces or JUST tabs should never be a topic of discussion. The only way I’ve found so far that can allow us to respect everyone’s coding standards and play nice with github is…. dun dun dun..

Use tabs at the beginning of a line and spaces everywhere else

Alot of developers seem to have adopted this, heck even JSHint at default warns you about this (kinda). If you’re OCD like me and prefer your code to line up nicely, that’s fine, but please don’t mix spaces and tabs. After your first non-tab character in a line, try to resist hitting space, then tabbing a bunch of times just to line something up.

If you’re a Vim user, here’s a great script that will automatically use spaces instead of tabs when you try to tab after a non-white character. It will even line up with the indentation above using spaces! Just throw in your vimrc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function VaryTabs()
  if &expandtab
    return "\<Tab>"
  else
    let nonwhite = matchend(getline('.'),'\S')
    if nonwhite < 0 || col('.') <= nonwhite
      return "\<Tab>"
    else
      let pos = virtcol('.')
      let num = pos + &tabstop
      let num = num - (num % &tabstop) - pos +1
      return repeat(" ",num)
    endif
  endif
endfunction
inoremap <Tab> <C-R>=VaryTabs()<CR>

Comments