PDA

View Full Version : Tabs or Spaces



DavidP
04-05-2007, 10:46 AM
I would have to say that one of my biggest pet peeves in programming is when I use a text editor or an IDE that defaults to inserting "tabs" by inserting a certain number of spaces, instead of inserting an actual tab character. It is incredibly annoying.

Why? Because I tend to switch between editors, computers, and OS's quite often. I could be programming on my Windows XP machine at home using either Visual Studio, SciTE, or Code::Blocks, and then later that same day I could be on campus using a Linux machine using and using Kate as my editor...all while editing the same program or application.

It is annoying to switch between two different editors, and to have to account for how those editors insert their tabs! For example, Visual Studio, SciTE, and Code::Blocks ALL insert some random amount of spaces instead of actual tab characters. You can specify in the IDE's settings to have it insert tab characters instead...but usually you are too late. Usually you have already coded some amount of code before you remember to turn on that setting. Often times you have to turn on that setting EACH time you open the IDE (like with SciTE)...which is really annoying...I believe it is the same with Code::Blocks as well.

So then...going from SciTE or Code::Blocks, which insert spaces, I move over to a Linux machine and open up Kate, and it inserts tabs instead! Of course now all the indentation is going to be off, because half of the code was done using one editor, and the other half using the other editor...and so the code looks like a complete mess now!

If all editors would just insert tab characters instead of their own pre-defined amount of spaces, it would make life so much easier. You could open your code in any editor, and the indentation would all be the same. It would be nice, neat, and organized.

Go tab characters! Why on earth do so many editors default to using spaces?

Daved
04-05-2007, 11:05 AM
It's usually the other way around. People prefer spaces because they go back and forth between editors and tabs on some editors are the equivalent of four characters, but other editors have them as 2 or 8. Putting spaces means that the code will line up the same everywhere you go.

However, I tend to prefer tabs, mostly because I generally use the same editor (MSVC++ which defaults to tabs for me) and I otherwise can change the settings to make 4-character tabs on any new editor I switch to. I use arrow keys to maneuver inside code and I moving left three tabs is much easier than moving left 12 spaces.

Salem
04-05-2007, 11:09 AM
Spaces all the way man!.

Sooner or later, you'll come across some tool or other (for example, a message board) which makes a complete mess of handling tabs.

Or it will display tabs, but only in the default width of 8 spaces.

Your smart code editor with tabs set at 4 spaces may indent both these to the same level
<s><s><t><t><s><s>printf("hello");
<t><s><s><s><s><s><s>printf("world");
But as soon as you post it on a message board, voila, instant garbage


printf("hello");
printf("world");

We see this kind of stuff all the time, people using random mixtures of both post their nice code and we all see puke.

Spaces are perfect for indentation, pure vanilla, tastes the same everywhere.

An example from work.
One well edited file I recall recently used tabs all over the place. Part of the file made sense if you set the tab stops to 2, another part 4 made more sense and still other places it seems that 8 made the most sense - OMG.

DavidP
04-05-2007, 11:16 AM
But that is exactly my point. When one editor inserts "4 spaces" for tabs, and another editor inserts "2 spaces" for tabs...all you see is puke.

you will see something like:



// Check if the database is available.
if (!$dbm->isConnected()) {
<s><s>print 'Unable to access the database at this time.<p>Sorry for the inconvenience.';
<s><s>exit;
}
else print 'Connected!';

if ( !isset($_SESSION['dp_account']) )
<s><s><s><s>$_SESSION['dp_account'] = new Account();


Those two if statements are indented differently because two editors choose to handle tabs differently. If both would insert tabs:



// Check if the database is available.
if (!$dbm->isConnected()) {
<t>print 'Unable to access the database at this time.<p>Sorry for the inconvenience.';
<t>exit;
}
else print 'Connected!';

if ( !isset($_SESSION['dp_account']) )
<t>$_SESSION['dp_account'] = new Account();


Everything is neat! Everything is indented the same!

Now obviously each editor can choose individually, or let the user choose, how much white space there actually is per "tab character"...but my point is that the actual characters inserted into the file should be tabs in order to prevent all the confusion.

JaWiB
04-05-2007, 11:20 AM
That's why you set the IDE to insert a specific number of spaces for tabs (rather than using the default)

DavidP
04-05-2007, 11:27 AM
Once again we return to my point: I use, as probably most of you do, more than one editor and IDE. I switch between them often. Many of these editors require you to set those certain settings (like SciTE) each time you open the editor, which is rather annoying. They don't always remember your previous settings. Let each editor insert tabs into the file (it can display however much whitespace it/the user wants as long as it inserts a physical tab), and all will be nice and neat.

laserlight
04-05-2007, 11:41 AM
I generally stick to spaces for indentation and formatting of code. However, when it comes to the default, I do not care, since I always check the settings before coding on an editor that I have not yet configured.


Everything is neat! Everything is indented the same!
Consider this code snippet:

std::transform(a.begin(), a.end(),
b.begin(),
foo());
It so happens that "std::transform(" is 15 characters. Suppose you use tab characters, and set the size to appear as 4 spaces. To indent "b.begin()" and "foo()" such that they line up with "a.begin()", one might use 3 tabs, followed by 3 spaces. Now, once we switch to an editor where the tab size is different, they no longer line up.

One way to solve this problem is to restrict tab characters to indent levels, and then use spaces for additional formatting. This was what I used before I had to submit code for university modules, upon which I decided that I could not trust the marker to change his/her editor settings for tabs to match mine :p

Note that this also means that you have to be very careful not to misplace your tabs while editing existing code, which can happen since both spaces and tab characters are whitespace. For example, you could use an editor that has the option of showing whitespace to distinguish between spaces and tab characters. I suspect that as a programming team grows larger, it becomes more difficult to ensure that everyone is as careful, and so it may be easier to just enforce an "all spaces" rule.

DavidP
04-05-2007, 12:28 PM
Well laserlight, your case is an exception. In such a case I would honestly probably just hold down the space bar until the right spacing was acquired.

For the large majority of cases we are indenting if-statements, loops, function bodies, and class definitions. In those cases it is highly annoying to move from editor to editor with different spacing rules. I can't believe so many of yall use pure spaces...they frustrate me to no end because the indentation is always off. I have noticed that most Windows editors/IDEs use spaces by default, and most Linux editors/IDEs use tabs by default.


Case Study:

Even though we are sitting here on a C/C++ forum, lets imagine we are programming in Python, which is parsed and defined purely by spaces.

Using "spaces" could easily confuse the Python interpreter...and indeed it has been done. Take two interpreters, one that uses 4-space tabs, and the other that uses 2-space tabs.

Take the following code:


class MyClass:
def myFunction ( ):
print 'Hello'
print 'Goodbye'


There is a class declaration on line 1. Line 2 contains 1 tab, and a function definition. Lines 3 and 4 contain two tabs, and code statements.

Lets say for example that the first two lines were done using editor 1, and the second two lines using editor 2, it would end up looking like this:



class MyClass:
<s><s><s><s>def myFunction ( ):
<s><s><s><s>print 'Hello'
<s><s><s><s>print 'Goodbye'


This would break the interpreter. Using tabs:



class MyClass:
<t>def myFunction ( ):
<t><t>print 'Hello'
<t><t>print 'Goodbye'

laserlight
04-05-2007, 12:41 PM
In those cases it is highly annoying to move from editor to editor with different spacing rules.
I note that "spaces by default" is as effective a standard as "tab characters by default".


I can't believe so many of yall use pure spaces...they frustrate me to no end because the indentation is always off.
How can the indentation be off? The bad thing about the "pure spaces" approach is that you have to delete all those spaces instead of just a tab character, and that the file size is larger. The indentation is never off, even in the "exception" that I described.


I have noticed that most Windows editors/IDEs use spaces by default, and most Linux editors/IDEs use tabs by default.
Notepad and Notepad++ use tab characters by default, as does MS Visual C++ 2005 Express, if I remember correctly.

DavidP
04-05-2007, 12:47 PM
Notepad and Notepad++ use tab characters by default, as does MS Visual C++ 2005 Express, if I remember correctly.


Notepad does use tabs. Visual C++ as far as I know defaults to spaces...I haven't verified that, but I assume that because the tabs never line up when I bring it into VIM or Kate, which both default to tabs.



How can the indentation be off?


See my above posts...especially the case study above. I am not talking about when you strictly use one editor, because then all is conformed to that one editor. I am talking about a multi-editor situation...which I find myself often in, as most of you probably do as well.

Daved
04-05-2007, 12:50 PM
>> Visual C++ as far as I know defaults to spaces.
I have used three different versions of Visual C++, and I have never specifically changed this setting. It always uses tabs for me.

I do change keyboard and layout settings to VC++ 6.0 style, so it is possible that doing so changes from tabs to spaces, but otherwise it defaults to tabs for me.

laserlight
04-05-2007, 12:50 PM
Using "spaces" could easily confuse the Python interpreter...and indeed it has been done. Take two interpreters, one that uses 4-space tabs, and the other that uses 2-space tabs.
I note that PEP 8 (http://www.python.org/dev/peps/pep-0008/) recommends:
"Use 4 spaces per indentation level."


See my above posts...especially the case study above. I am not talking about when you strictly use one editor, because then all is conformed to that one editor. I am talking about a multi-editor situation...which I find myself often in, as most of you probably do as well.
Ah, but I could just as easily state that: I can't believe so many of yall use tab characters...they frustrate me to no end because the indentation is always off.

The problem lies in the mix of spaces and tab characters in ways that cause indentation to be misaligned. It does not lie in either the "pure spaces" or a tab character approach per se.

Ken Fitlike
04-05-2007, 12:55 PM
>>like SciTE<<

You can set it once in SciTEGlobal.properties - I can't remember exactly where, but the various 'tab' settings under 'indentation' are the ones to fiddle around with; check out the SciTEDoc.html file in your installation directory which should provide explicit information.

QuestionC
04-05-2007, 12:55 PM
When I first started, I picked tabs. I thought it was inane to have to hit space 12 times versus hitting the tab key three times. Besides, I didn't -want- the option to perform a non-consistent amount of indenting.

As I got to know the ropes on my editor, the difference between tabs and spaces grew more transparent with auto-indenting. Tabs was still better, IMO. When I wanted to un-indent a line by 1 shift, I hit backspace once, it unindented.

Before long, I found out my editor could just retroactively re-indent the whole file whenever I want, so the whole issue is now moot. When a file's mis-indented (usually not due to tabs), I just type in 4 characters and everything's perfect.

QuestionC
04-05-2007, 01:08 PM
I note that "spaces by default" is as effective a standard as "tab characters by default".
Not exactly true.

"Spaces by default" represents about half a dozen standards, each of which is inconsistent with each other...
2 Spaces per indent
3 Spaces per indent
4 Spacer per indent
&ct...

If I edit a file written by someone who used 5 spaces per indent, I either have to change how I typically indent for just that file, or completely re-indent the entire file.

"Tabs by default" is unambiguous, and lets different programmers choose their amount of indenting without physically modifying the entire source.

Perspective
04-05-2007, 01:17 PM
>See my above posts...especially the case study above.

the case study is a user error, not a problem with the number of spaces representing a tab. If you open the python file in a new editor and your 2 indents line up wrong, you should re-indent the file, or change your editor preferences to be consistent.


all spaces, all the time :D

divineleft
04-05-2007, 01:20 PM
tabs

it's horribly impracticable to press the space bar 4 times when you can just press tab. same applies to deleting things from lines.

and if portability is such an issue, just change the tab width to meet your requirements.

i don't post very much code on forums, and when I do, it is mostly example code that I write right then and there. i'm not uploading files or anything. if i ever had to do something like that and the spacing was messed up, I would just make a short script or c program to change tabs into spaces.

laserlight
04-05-2007, 01:39 PM
"Spaces by default" represents about half a dozen standards, each of which is inconsistent with each other...
You're right. To be absolutely consistent such a standard has to define a number of spaces per indent level, similiar to the recommendation in Python's PEP 8 document. Of course, the difference is still clear from the start, as when you try to indent, you would immediately notice that the indent level is inconsistent with the original code.


"Tabs by default" is unambiguous, and lets different programmers choose their amount of indenting without physically modifying the entire source.
I liked that reasoning, but when I discovered "x characters per line" limits due to printing and screen line limits, I realised that the choice is not always there. Changing the indent size of a tab can break such guidelines, and one also has to consider the printer settings if the code is to be printed. You really do have to follow the convention set out in the original code, or else just re-format everything.

MacGyver
04-05-2007, 01:44 PM
Tabs. Something just irritates me about trying to delete a tab character by pressing backspace only to find out that the tab is actually multiple spaces. :)

cboard_member
04-05-2007, 02:19 PM
Spaces. I didn't care the begin with, but after a few hundred buggered forum-posts, I turned into a space-man.
EDIT:
>> Tabs. Something just irritates me about trying to delete a tab character by pressing backspace only to find out that the tab is actually multiple spaces.

I do see your point about that though, especially since I just upped my indentation level from 2 to 4 spaces. Maybe I should become tab-man again.

whiteflags
04-05-2007, 02:59 PM
Tab-man.

Actually, I usually cheat. If I brace everything fully, the IDE will indent with spaces automagically. So I guess my indenting style is braces.

I would think though that if you were working on a serious project, that these sort of guidelines are hammered out and you're required to do it that way. I've also recently decided that stuff like


std::transform(a.begin(), a.end(),
b.begin(),
foo());

is just not worth it. I don't need it to be that neat and I don't try to. You'd have to make me.
And I wonder, why is a.begin() and a.end() on one line? [/rhetoric]

twomers
04-05-2007, 04:20 PM
Up until very recently I was a tabber all the way (though I also open and close scope at the start and let the ol' IDE do most of the magic for me), but then I decided to change from tabs to spaces which I prefer. The only problem is using old code and new code and the tab-space mix ups :/ meh. Hopefully someone will be gracious and suggest a good fix for me ...

Sang-drax
04-05-2007, 04:31 PM
With many people working on the same source files, I found that tabs was far superior to spaces. Different people like different indentation widths. I used to like spaces more, but now I've come to my senses. :)

Perspective
04-05-2007, 05:06 PM
>it's horribly impracticable to press the space bar 4 times when you can just press tab

erm, we're talking about editors that place x number of spaces in the file when you push the tab key.

>>> Tabs. Something just irritates me about trying to delete a tab character by pressing backspace only to find out that the tab is actually multiple spaces.

A good editor will take care of the indenting for you, in emacs for example, you can push the tab key with your cursor in any position on the line and it will properly indent the line. No need to explicitly add/delete tabs or spaces.

divineleft
04-05-2007, 05:25 PM
lol

emacs

Dave_Sinkula
04-05-2007, 06:17 PM
I would have to say that one of my biggest pet peeves in programming is when I use a text editor or an IDE that defaults to inserting "tabs" by inserting a certain number of spaces, instead of inserting an actual tab character. It is incredibly annoying. Consider using a beautifier (http://en.wikipedia.org/wiki/Code_beautifier#Pretty_printers). Then you can write in a relatively sloppy manner, and if it's as simple as a toolbar button (it is my case), then click it every couple lines or so to make everything not only pretty but consistent.


>it's horribly impracticable to press the space bar 4 times when you can just press tab

erm, we're talking about editors that place x number of spaces in the file when you push the tab key.

>>> Tabs. Something just irritates me about trying to delete a tab character by pressing backspace only to find out that the tab is actually multiple spaces.

A good editor will take care of the indenting for you, in emacs for example, you can push the tab key with your cursor in any position on the line and it will properly indent the line. No need to explicitly add/delete tabs or spaces.Echo that.

[edit]And spaces, by the way. Three or four.

Decrypt
04-05-2007, 09:05 PM
Spaces FTW. 3 of 'em.

I'll admit I didn't read every one of DavidP's posts, but I totally agree with this:
Putting spaces means that the code will line up the same everywhere you go.I use Code::Blocks at home for the most part, pico (I don't know why I love pico when I could use emacs instead, it makes no sense at all), and notepad at work. Going between the three is easy easy easy since I have Code::Blocks convert tabs to spaces. That way it's always the same.

That said, if you're going between editors that are "good" (as defined above), it probably doesn't matter as much.

VirtualAce
04-05-2007, 11:18 PM
Spaces.

Tabs destroys your code in any other editor. Spaces are universal and every editor should know what spaces are and how many to use. Tabs are relative to the app and settings in the editor displaying the document.

And besides, cprog totally obliterates any source code that uses tabs instead of spaces.

laserlight
04-06-2007, 12:09 AM
is just not worth it. I don't need it to be that neat and I don't try to. You'd have to make me.
As you noted, it ultimately depends on the coding standards of the project that you are working on, or the company that you are working for.


And I wonder, why is a.begin() and a.end() on one line? [/rhetoric]
That is a visual indicator that they are actually one argument (a range) divided into two (the start and end of the range).

KONI
04-06-2007, 01:16 AM
Probably every major Coding Standard guide I found recommends using 4 spaces instead of tabs, just like the Pear PHP (http://pear.php.net/manual/en/standards.php#standards.indenting) Coding Standards Guide:



Use an indent of 4 spaces, with no tabs. This helps to avoid problems with diffs, patches, CVS history and annotations.

DavidP
04-06-2007, 01:29 AM
Spaces are universal and every editor should know what spaces are and how many to use. Tabs are relative to the app and settings in the editor displaying the document.


I would say it is the complete opposite. Tabs are universal. Any editor can read a tab, and know that there should be some amount of specified indentation.

NOT all editors agree on what spaces are and how many to use, and that is exactly the problem. Tabs are NOT relative, but are universal. Spaces are relative to the editor.

Hence the options that occur in most editors: "Spaces per tab indentation"....because each editor does it differently.


Oh...and I have never experienced the board messing up tabs. The python code I posted earlier used tabs...looks great to me.




I would have to agree with laserlight that for the most part it is the mixture of tabs and spaces that really obfuscates code. Tabs and spaces simply dont go together. If yall want to use spaces, then that is your choice, but I would have to agree that a mixture of the two just doesnt work.

But when it comes down to choosing either spaces or tabs, it's tabs all the way.

KONI
04-06-2007, 01:34 AM
On the contrary ... a space has a fixed width of "space" while a tab has a relative width. Just like in Word you can chose your own tab width. 4 spaces will always look the same, regardless of what editor you use, since it depends only on the font size. Tabs on the contrary depends on your application.

twomers
04-06-2007, 03:19 AM
It really depends on who is going to see it really, doesn't it? I mean, for c-prog it's good to have four (etc), spaces cause it doesn't change the code to being "uber spaced", I think ethic has a problem with that, so I'm going to say he likes spaces too. However, if you're just writing code for yourself I don't think it matters what you use, I'd wager that whatever your IDE came defaulted to is what you love most. Alternatively if you're writing code for the "man" it's whatever he wants that you're going to use regardless of which you prefer.

It took me a while to realise the importance of code indenting, pre-cboard time though. I'm sure we're all of the same mind that badly indented code is bad, right? Then why say that universally well indented code isn't the best?

edit:
>> Oh...and I have never experienced the board messing up tabs. The python code I posted earlier used tabs...looks great to me.
Consider code that people copy at .. say ... 7 {}'s in with long commenting ... *shudder*

Queatrix
04-07-2007, 10:06 AM
>> Consider code that people copy at .. say ... 7 {}'s in with long commenting ... *shudder*
:D

The votes are pretty close, I really dont see why people dont see the problem with tabs...
I personaly use three spaces to indent rather than four, but even then, the Hbar get to long.
DavidP, should've made the poll public. ;)

Salem
04-07-2007, 11:06 AM
Get a decent code editor, then

It will indent code by your chosen tab stops (insert multiple spaces) when you press tab.
It will automatically indent the next line when you press return.
It will re-indent code as you copy/paste it if necessary.
And so on and so on...

You people pressing space 12 times just to indent 3 tabs stops really need to figure out how the tools work (or pick a better strawman to knock down).

Daved
04-07-2007, 11:50 AM
How do you delete those 12 spaces? You have to select them and delete or hit backspace 12 times. How do you use the arrow keys to maneuver through those spaces?

Spaces are simply more cumbersome than tabs. The reason people prefer spaces is that their other advantages outweigh that cumbersomenessosity.

twomers
04-07-2007, 12:53 PM
>> How do you use the arrow keys to maneuver through those spaces?

How do you delete the spaces? ctrl + backspace!! :) Or if you just want to maneuver through a word or so, ctrl + DIRECTION KEY should do it. SPACES FOR PRESIDENT!

dwks
04-07-2007, 04:16 PM
I personally use spaces all of the time, never tabs. But I have a nice editor that indents code for me; I only have to hit the spacebar four times at the beginning of a block. If I was using notepad I might be tempted to use tabs . . . :)

Plus the extra spaces increase your source file size to make it look more impressive. :)

Dave_Sinkula
04-07-2007, 11:49 PM
How do you delete those 12 spaces?I usually don't unless they're in comments. I generally click on this button, which also has the effect of reformatting the whole file if nothing is selected (or only the selection):

laserlight
04-08-2007, 01:25 AM
SPACES FOR PRESIDENT!
We're winning! Just a few more votes and we'll have a sufficient majority to elect spaces pope! heheh :)


Plus the extra spaces increase your source file size to make it look more impressive.
I thought that was a downside to using a "pure spaces" approach, actually... but I guess it depends on your point of view ;)


I usually don't unless they're in comments. I generally click on this button, which also has the effect of reformatting the whole file if nothing is selected (or only the selection)
What text editor or IDE do you use?

Queatrix
04-08-2007, 09:39 AM
Most of the time, it doesn't matter wether you use tabs or spaces, 'cause you only have to use them at the beggining of an indentation, the IDE will do the rest for you.

>> We're winning! Just a few more votes and we'll have a sufficient majority to elect spaces pope! heheh
How many votes is that?

BTW:
>> SPACES FOR PRESIDENT!
:D

twomers
04-08-2007, 09:41 AM
>> How many votes is that?
Duh! 42 ... Can you do anything about that, Salem :p

Queatrix
04-08-2007, 09:46 AM
Okay,.. why 42???




>> Duh! 42 ... Can you do anything about that, Salem ;)
:D

twomers
04-08-2007, 09:49 AM
>> Okay,.. why 42???

You jest ... ?

laserlight
04-08-2007, 10:05 AM
Nah, 42 is good, but we only need 24 at the moment.

whiteflags
04-08-2007, 10:40 AM
>> Okay,.. why 42???
Because it's the answer to life, the Universe, and everything. Strangely though, it usually has nothing to do with pope. If we're going to elect spaces pope, than we'd require a two-thirds plus one majority, and no one in the conclave can turn eighty. We've also already messed up the balloting process.

Dave_Sinkula
04-08-2007, 11:45 AM
What text editor or IDE do you use?SlickEdit