*** MOVED ***

NOTE: I have merged the contents of this blog with my web-site. I will not be updating this blog any more.

2006-01-04

Confessions of a TAB-hater

I do not like it when people put TAB characters in their source code for indentation. I prefer to use spaces instead of TAB characters to indent my source code. Jamie Zawinski has written a nice document that clearly explains the problem. Note that I am not against using the Tab key as a quick way of indenting, but putting in actual TAB characters (ASCII character #9) in the source code. Different people set tabstops differently, so what looks good to them looks terrible to others. These characters also mess up the indentation in patch diffs.

VIM has an unfortunate optimisation that replaces consecutive spaces with as many TAB characters as possible and if you "set expandtab", then even desirable TAB characters (in Makefiles for example) are replaced by spaces. Yes, I know of using the Ctrl-V + Tab sequence to insert an actual TAB character in this mode, but it is painful. So I have ended up putting the following in my ".vimrc":

set shiftwidth=2
set tabstop=8
inoremap <Tab> <C-V><Tab>
autocmd BufNewFile,BufRead *.java setlocal expandtab
autocmd BufNewFile,BufRead *.c setlocal expandtab
autocmd BufNewFile,BufRead *.cc setlocal expandtab
autocmd BufNewFile,BufRead *.cpp setlocal expandtab
autocmd BufNewFile,BufRead *.h setlocal expandtab
autocmd BufNewFile,BufRead *.hh setlocal expandtab
autocmd BufNewFile,BufRead *.hpp setlocal expandtab
autocmd BufNewFile,BufRead *.xml setlocal expandtab
autocmd BufNewFile,BufRead *.sql setlocal expandtab

If I were to design a programming language, I would seriously consider not allowing TAB characters to be counted as whitespace. It would make source code use up a few extra bytes compared to what it would if consecutive spaces are replaced by as many TAB characters as possible, but I would trade that off for overall sanity.


By the way, I consider it really silly that the authors of make chose to make TAB characters so important, when they are virtually indistuingishable from spaces when you inspect Makefiles. Their mistake has cost the software world the creation of the monstrosity named Ant.

(Originally posted on Advogato.)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.