Spaces vs. Tabs? 32
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?
Not that hard to switch from one to the other (Score:1)
Re:What about the wrong spaces? (Score:2)
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.
What about the wrong spaces? (Score:2)
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)
Tabs Permit Individual Preferences (Score:3)
Waldo
spaces .. tabs are unpredictable (Score:2)
I don't want a lot, I just want it all!
Flame away, I have a hose!
Re:WHO CARES? Just use GNU indent(1) when done (Score:1)
The disk space argument (Score:2)
Same guy that made that decision went on to declare that indents should be *3*. Not 2 or 4.
Re:Ask JWZ (Score:1)
That only works for the first column. What about those things lined up on the right?
Re:WHO CARES? Just use GNU indent(1) when done (Score:1)
general indention? tab! (Score:1)
The clincher is that _you_have_to _learn_to_use_your_friggin_tool!_ If you don't know how to get lpr, less, or vi to show your indentions how you want, then you should RTFM!
Re:The disk space argument (Score:1)
Re:Thus spake Torvalds (Score:1)
Re: GNU indent makes mistakes (Score:1)
Not only that, but GNU indent has no way to indent with spaces instead of tabs.
Summary: GNU indent is bad.
Ask JWZ (Score:3)
Join us next week for stories such as "Jewish vs. Christian", "Emacs vs. vi", and "PERL vs Python"
Re:I use spaces (Score:1)
trying not to start another holy war in the same thread, but what editor do you use?
--
Re:The disk space argument (Score:1)
Point being, choice of tab size really depends on the langauge :)
Are you serious ? (Score:2)
Where i work its always, "Umm, the boss just gave us until morning to ..."
:)
I use spaces (Score:4)
Re:Tabs Permit Individual Preferences (Score:2)
Thus spake Torvalds (Score:3)
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.
tabs.... (Score:1)
at my work, my boss made the policy tabs, with exactly one tab before any comments that happen to be on the same line as code. this way everyone writes the same code, and they can setup their editors to use their preferred number of spaces.
html (php included) has no policy however, so everyone that has to do any of that gets to deal with idiot code. i tend to put all of my html/php onto one single line. i wish i could do the same for c.....
.brad
Drink more tea
organicgreenteas.com [organicgreenteas.com]
Do your own thing (Score:1)
I'm a vi junkie (Score:2)
-----
Comments to tell what the tab was (Score:1)
WHO CARES? Just use GNU indent(1) when done (Score:3)
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):
emulated tabs (Score:1)
The feature works well enough that I'm rarely annoyed by having to deal with many space characters rather than single tab characters. Besides, if I ever want to change the indentation of a block of code, I can just highlight it all and indent/deindent it all at once. So, spaces or tabs make little difference.
I'm sure other editors can do this, but if they didn't, I'd probably use tabs and put a comment at the beginning of the code to indicate what the tab setting was when the document was created. (It's no fun having to figure that out at some later date.)
Re:tabs.... (Score:1)
When I'm doing php, I do it like this: With the php code indented. Different strokes for different folks...
Re:WHO CARES? Just use GNU indent(1) when done (Score:1)
Re:Ask JWZ (Score:1)
No, it's merely shortsighted and moronic.
It all comes down to an elaborate defense of the shortcomings of his preferred editing platform.
The fact is, tabs are the One True Way. Any other approach condemns the entire development team to using the same indentation, despite the deleterious effects it may have on their work (I have a really hard time with large indentation like 8 spaces or so, and many other people have a hard time with the 2 spaces that I prefer). With tabs, we can all set our editor preferences as we please, and freely work in each other's wakes without reformatting, screen-squinting, or other hassles.
Re:spaces .. tabs are unpredictable (Score:1)
Back in my C days, I used tabs set to three. Now I use four. In the legacy code I occaissionally pull up where some idiot went through and converted my tabs to (3) spaces, it's a mess.
--
Re:Tabs Permit Individual Preferences (Score:1)
Tabs serve a different purpose to spaces. When indenting use tabs. When spacing use spaces. The only reason for spacing in is stupid editors with queer indentation.
Spaces with automatic indent/unindent (Score:1)
To my surprise I found that delphi understands C syntax and does highlighting if the file suffix is ".c".