#### Archived

This topic is now archived and is closed to further replies.

# Tab or No Tab :: C++ Compilers

## Recommended Posts

Hi, I program using Visual C++, vi, some occassionally using KDevelop. I usually experience problems with tabs and spaces. I use tab (3 spaces) in Visual C++. However, vi almost always open the source files with random spaces. In general, do developers use spaces instead of tabs? Thanks, Kuphryn

##### Share on other sites
It has been my experience that most programers are unaware that tabs and spaces are DIFFERENT FRIGGIN'' CHARACTERS and also unaware that tabs can be displayed with DIFFERENT FRIGGIN'' SIZES.

I''m maticulous when aligning code to ensure that it looks correctly no matter what the tab size is. A few ''auto-type''/''auto-tab'' features of MSVC6 are broken in this regard, where they insert tabs where you really need spaces.

Like this code:
  functioncall(with, a, pile, of, parameters);

MSVC6 will use tabs to fill inthe blank space by default. Perhaps vi/emacs/kDev all have this "feature".

There should be one, and exactly one, tab at the _start_ of each line, and then spaces to line up the call. Seems really trivial to me, and seems to be ignored by everyone else (who''s code I''ve looked at).

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor
There should be one, and exactly one, tab at the _start_ of each line, and then spaces to line up the call. Seems really trivial to me, and seems to be ignored by everyone else (who''s code I''ve looked at).

Never knew/heard that. I''ll take it into consideration henceforth.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor
There should be one, and exactly one, tab at the _start_ of each line, and then spaces to line up the call. Seems really trivial to me, and seems to be ignored by everyone else (who''s code I''ve looked at).

You''re not the only one who''s worried about things like that. I indent in the same manner: so that no matter the size of the tabs everything is aligned correctly.

Kuphryn

##### Share on other sites
This problem can be easily fixed in MSVC...

Tools->Options->Tabs... Set auto-indent to "Default", tab-size and indent-size to the same thing (I use 4) and choose "insert spaces".

MSVC is pretty smart about handling this. When you press TAB, it''ll move to the next TAB location (in my example, a multiple of 4). If you hit then hit BACKSPACE, it''ll back-up through one space at a time. But, in a situation where MSVC has inserted a TAB for you, for example when you hit RETURN at the end of a line and it continues at the same indent (true for SMART and DEFAULT auto-indent), but then hit BACKSPACE, it will skip back a whole TAB''s worth. Definitely useful when you''re closing off a brace and what scoot back at the end of a block.

And now, when you load up the file in any editor the spacing will be the same, because there are no TAB characters (unless those characters were already in the file before you specified the above options). I believe MSVC will convert TABS to SPACES when you enable the above options and then resave the file, but I may be wrong...

##### Share on other sites
I agree with the usual action: only use tabs to line up the "depth level of the code". It''s usefull for loops indeed. However, would it not also be acceptable to include a comment at the beginning of your main.cpp, stating the tab-number you used...?

##### Share on other sites
I agree w/ Simagery. Always use spaces. Set up your editor so that it inserts spaces when you hit the Tab key. Then there is no issue.

The only text files that should have tab characters in them are ones that require them (like makefiles).

##### Share on other sites
Tabs are evil and the tool of the devil. Try to edit a file that''s been around for 10 years and editted by different people with tabs set to 2, 3, 4, and/or 8 and you''ll agree.

The size savings for using tabs is worthlessly trivial and there''s no point in having two rules (tab, then spaces) when one will do (only spaces).

-Mike

##### Share on other sites
Thanks everyone.

I set Visual C++ to use *spaces* when I hit the tab key. This method works great. Thanks!

Kuphryn

##### Share on other sites
Argh, if you set it to insert spaces then you force everyone else who looks at the code to use your indentation level... I suppose that''s better than idiotically tab aligned code...

Tabs to indent! Spaces to align! It''s really not that hard...

Tab is an ascii character (0x09), no need for binary.

##### Share on other sites
Everyone should use my indentation level. My indentation is the One True Indentation.

Once there was a time when all people believed in God and the church ruled. This time is called the Dark Ages.

##### Share on other sites
Use a code formatter like astyle.

##### Share on other sites
I''m confused, as I am most of the time.

I always use tab to indent. It shouldn''t cause problems
cause whatever opens it will treat every tab the same so
the indentation should always be neat but what do you mean
by aligning stuff with spaces? If you use tabs to indent,
what''s left to do?

(I prefer tabs 8 spaces long. As far as using spaces to
indent, there''s no way I''m gonna take the time to push a
key three times when once will suffice nor take the time
to set up an IDE to do it automatically and I use the
free borland compiler which is command-line so I use

##### Share on other sites
Roland - some applications will not have your tabs as 8 spaces so the indentation will be messed up, if you indent things to match up to word lengths rather than an arbitrary number of tabs.

Having said that, I use tabs rather than spaces, and always will unless my employers decree otherwise. I find it so much easier to reformat code when it''s tabbed, and it reduces the chance of me clicking in the wrong place.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
Tabs forever!

Unfortunately I''m confused again.
quote:

Roland - some applications will not have your tabs as 8 spaces so the indentation will be messed up, if you indent things to match up to word lengths rather than an arbitrary number of tabs.

Are we talking right-justified?
Yes indeedy tabs take up a different number of spaces
depending on application and its settings, but what is this
matching to word lengths?

##### Share on other sites
See Magmai''s example? If your tabs are 4 characters in size, you''d require 3 tabs and 1 space to align it like that. If your tabs are 8 characters wide, you need 1 tab and 5 spaces to align it. Now, once you''ve aligned it, change the width of your tabs, and everything gets out of alignment. Of course, you''re unlikely to change the width of your tabs, but passing the file to someone with a different setting will have this effect.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
quote:

Of course, you''re unlikely to change the width of your tabs, but passing the file to someone with a different setting will have this effect.

Or merly opening it in another editor on your machine, or on a fellow employee''s machine.

There should be the same number of tabs at the beginning of every line of indented code.

Use tabs to indent, spaces to fill-space. This is important because you use single-width characters (anything but a tab/lf/cr really) on the code above. Your own code won''t look right if you decide to change editors, or one day decide that 8spaces/tab is too many, and only want 5.

quote:

Having said that, I use tabs rather than spaces...

Arghhh, doesn''t it bother you that it only looks right for one case? When it could be right for all of them?

##### Share on other sites
It would do, if the first thing I did with every editor wasn''t to set the tab width to 4

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
I use tabs for just about any alignment of multiple lines, and spaces for whitespace between lines.

Sometimes when I''m just doing experiments I use lots of OpenGL function calls like this:

  //I type this:glVertex3f(1.0f,---TAB---1.0f,---TAB---2.0f);glVertex3f(12.32f,-TAB---992.38,-TAB-100.2f);//so that it looks like this:glVertex3f(1.0f, 1.0f, 2.0f);glVertex3f(12.32f, 992.38, 100.2f);

I really like the way that works.

The real problem is that people just aren''t consistant. They do things like this:
  ---TAB------TAB------TAB----cout << "Hello\n";-space--space--space--space-cout << "Godbye\n";

If you used all tabs or all spaces, it would be fine. In this case, you chould use tabs.

So basically, I disagree with those who say don''t use tabs at all; I just maintain that you shouldn''t mix and match.

• ### Forum Statistics

• Total Topics
628329
• Total Posts
2982105

• 22
• 9
• 9
• 13
• 11