Spaces vs. Tabs?
Mike Hall asks: "It has come up at work again. We are about to start a new design on our product and the spaces vs. tabs discussion is back. When you indent c/c++ code should all spaces, or tabs be used? Almost everyone agrees that a mix of the two is a bad idea. Currently most of the programmers here want to use tabs set to 4. I am a space junkie. I prefer all my code to look the same if I am looking at it on vi, emacs, less, or on a paper after a lpr command. What does the slashdot community feel is the pros and cons of spaces and tabs in code?" I'm kind of torn on the issue. I believe that properly indented code is important in improving readability, but I'm also a firm believer in the fact that to edit code, you shouldn't need to spend hours and hours on formatting. Tabs are the fastest way to align your data, but they have their drawbacks and formatting code with spaces takes for ever. What works best for some of you, out there?
Precisely! A tab is a single indent. That's completely portable across any environment. By far the biggest argument against using spaces (and I've seen it in *every* instance I've worked anywhere that uses spaces) is that people try to do it manually -- i.e., without having the editor automatically expand tabs. And that leads to them getting it wrong! Try maintaining code written five years ago when the indentation varies between 2 and 9 spaces in the same file. It's not much fun.
"The invisible and the non-existent look very much alike." -- Delos B. McKown
One tab equals one indent. What does eight spaces equal? One indent? two? four?
:se ts=4 or :se ts=2 to your preference. If a file contains sequences of spaces there's no easy way to change it to your preference. I suppose you could have a collection of patterns that search for sequences of 2, 4, or 8 and replace them with a different number of spaces, but that's a lot more complex than just setting your tabstop preference once.
If you're writing code that no one else will ever see, then you can use whatever you prefer. What if someone who disagrees with you about indent level has to read your code?
If a file contains tabs you can easily
Spaces just don't seem practical if you have to share a file with someone whose preference is different from yours.
(I use vim)
You can set emacs, vi, and lpr to all make tabs equal whatever your preference is, in terms of size. Why force everybody to indent code with the same number of spaces when tabs let everybody make their code look like whatever they want?
Waldo
I don't want a lot, I just want it all!
Flame away, I have a hose!
Only 'flamers' flame!
Same guy that made that decision went on to declare that indents should be *3*. Not 2 or 4.
www.HearMySoulSpeak.com
Join us next week for stories such as "Jewish vs. Christian", "Emacs vs. vi", and "PERL vs Python"
Where i work its always, "Umm, the boss just gave us until morning to ..."
:)
Free Techno/Jazz/DNB/MI Music by guys obsessed with monkeys!
I use spaces in order to maintain legibility across editors. That said, I normally use editors which automatically auto-indent my code with spaces anyway so there wouldn't be any time saving for me to use tabs.
Tarsnap: Online backups for the truly paranoid
from /usr/src/linux/Documentation/CodingStyle:
Chapter 1: Indentation
Tabs are 8 characters, and thus indentations are also 8 characters.
There are heretic movements that try to make indentations 4 (or even 2!)
characters deep, and that is akin to trying to define the value of PI to
be 3.
Rationale: The whole idea behind indentation is to clearly define where
a block of control starts and ends. Especially when you've been looking
at your screen for 20 straight hours, you'll find it a lot easier to see
how the indentation works if you have large indentations.
Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen. The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.
In short, 8-char indents make things easier to read, and have the added
benefit of warning you when you're nesting your functions too deep.
Heed that warning.
Daniel
In my .vimrc I have "set tabstop=4" and "set expandtab" so the tabs are 4 spaces wide but are converted to spaces. I'm sure those using Emacs have a similar solution. I also use NetBeans for Java and it can also be set to do same things.
In HTML and XML I like to use just 2 space indentation because nature of hierarchical ordering of the tags makes for a lot of wasted space if you use more than that. I've seen people use only 1 space indent, and it's not too bad either.
-----
Of all the holy wars, this is the one that makes the least sense to me. One of the reasons we have tools is to make such useless pablum as unnecessary as possible.
Now, I speak as one who publishes source code on a regular basis, and finds that Mr. Torvald's edict works when editing but sucks when you are trying to publish on page. Never mind the fights with the "professional" graphic artists who don't understand the readers' needs, the rules according to Torvalds do not work on the published page. Witness Don Knuth's comments about white space management with respect to WEB source.
So let people do it the way they want. Tabs at 8? Tabs at 4? Spaces? No tabs at all? Fine. When you are done with the code, run it through indent(1) with the shop standard specification and be done with it.
And when I decide to write a book chapter using your Open Source code, I'll use my own configuration to make it easier to deal with on the printed page.
Oh, you want to know my standard? Just remember, you asked for it...
Courier 72 with monospacing, with a 66% set width
Braces on their own lines
Open and close braces at the same indent as the enclosed text
Each indent is equal to the type height, which is identical to a M-space followed by an N-space at the 66% setwidth, or an M-space if you ignore setwidth
bolding all appropriate keywords and certain macro variable names
Here is an approximate example (lack of proper typesetting controls in HTML prevent an exact depiction):