Indentation level woes

  • Thread starter Asfand Yar Qazi
  • Start date
A

Asfand Yar Qazi

Hi,

After much use of tabs worth 8 spaces, and constantly having to
format my C++ code in funny ways to keep all lines within an 80
character line length limit, I'm seriously thinking of switching
to using 4 spaces per tab. I feel like I'm about to lose a a
long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)

Thanks,
Asfand Yar
 
J

Jeff Schwab

Asfand said:
Hi,

After much use of tabs worth 8 spaces, and constantly having to format
my C++ code in funny ways to keep all lines within an 80 character line
length limit, I'm seriously thinking of switching to using 4 spaces per
tab. I feel like I'm about to lose a a long suffering faithful
companion...

What are your views on this? Any words of sympathy? :0)

Eight-column tabs are pretty much the one thing on which we've all
standardized. This isn't always convenient, but hang in there, man;
save the eight-column tab. :)

My solution has been to use spaces instead of tabs. Vim has helped a
lot; it has a concept of "shiftwidth" that makes the editor behave as
though tab were four spaces, but ^I actually inserts spaces instead of
tabs. Real tab stops are still set at eight-column intervals. In the
emacs modes I used to use, tab just aligns the current line according to
some variables you can set to control indentation.
 
W

Wellu =?iso-8859-1?Q?M=E4kinen?=

Hi,

After much use of tabs worth 8 spaces, and constantly having to
format my C++ code in funny ways to keep all lines within an 80
character line length limit, I'm seriously thinking of switching
to using 4 spaces per tab. I feel like I'm about to lose a a
long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)

The following paragraph is from Linux Torvalds, the
original author of the Linux kernel. This is the coding
style of the Linux kernel.

"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."

So you're not alone with 8-char indents..
--
|||
(0-0)
+--------oOO--(_)--OOo----------------------------+
| Wellu Mäkinen www.wellu.0rg |
| |
| No tears please, it's a waste of good suffering |
+-------------------------------------------------+
 
J

Jeff Schwab

Asfand said:
I didn't word my original intent correctly: I was proposing the same
thing as you, that 'basic-offset' as its known in emacs, is 4 spaces,
and whenever 8 spaces are concurrent to each other, they become a tab.

Why I am sad is that after using a 'basic-offset' of 8 spaces for so
long, I'm thinking of moving to a basic-offset of 4 spaces like Java (ugh.)

Thanks for the encouragement :0)

Ah, I see. Well, just think of all the line-wrapping you'll save!
 
A

Asfand Yar Qazi

Eight-column tabs are pretty much the one thing on which we've all
standardized. This isn't always convenient, but hang in there, man;
save the eight-column tab. :)

My solution has been to use spaces instead of tabs. Vim has helped a
lot; it has a concept of "shiftwidth" that makes the editor behave as
though tab were four spaces, but ^I actually inserts spaces instead of
tabs. Real tab stops are still set at eight-column intervals. In the
emacs modes I used to use, tab just aligns the current line according to
some variables you can set to control indentation.

I didn't word my original intent correctly: I was proposing the
same thing as you, that 'basic-offset' as its known in emacs, is
4 spaces, and whenever 8 spaces are concurrent to each other,
they become a tab.

Why I am sad is that after using a 'basic-offset' of 8 spaces for
so long, I'm thinking of moving to a basic-offset of 4 spaces
like Java (ugh.)

Thanks for the encouragement :0)
 
P

Paul

Asfand Yar Qazi said:
Hi,

After much use of tabs worth 8 spaces, and constantly having to
format my C++ code in funny ways to keep all lines within an 80
character line length limit, I'm seriously thinking of switching
to using 4 spaces per tab. I feel like I'm about to lose a a
long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)


I've used 4 for the last 10 years for the same reason you cite. I always
hated the way 8 space tabs look and they're not at all practical or useful.

The only thing worse is using (any number of) spaces instead of tabs, the
ultimate sloppy abomination. I worked with a contractor once who wanted that
(fortunately they WERE a contractor and couldn't tell me what to do.)

Paul
 
J

Jeff Schwab

Paul said:
I've used 4 for the last 10 years for the same reason you cite. I always
hated the way 8 space tabs look and they're not at all practical or useful.

The only thing worse is using (any number of) spaces instead of tabs, the
ultimate sloppy abomination. I worked with a contractor once who wanted that
(fortunately they WERE a contractor and couldn't tell me what to do.)

So for your code to be rendered properly, tab widths must be set to four
columns??? Disgrazia!
 
A

Andre Kostur

So for your code to be rendered properly, tab widths must be set to
four columns??? Disgrazia!

Personally I use 2 spaces for the indent.... but this is all a stylistic
choice. There is no "right" answer. (I also don't use tabs.. I use
spaces... and editors that will conform to my wishes.... :) )
 
S

Stewart Gordon

Paul wrote:
I've used 4 for the last 10 years for the same reason you cite. I always
hated the way 8 space tabs look and they're not at all practical or useful.

Join the club.
The only thing worse is using (any number of) spaces instead of tabs, the
ultimate sloppy abomination.
<snip>

Actually, worse still is inconsistently mixing tabs with spaces, as I
think tends to happen with some editors, and definitely happens from
time to time when different people and/or editors have worked on the
same code.

I suppose that, in terms of others reading your code, there are
arguments both ways. Using tabs means that they'll see your code
according to their comfort settings (assuming a half-decent editor).
But it also means that if their tabs are wider than yours, some lines'll
appear too long. But then again, their editor window might not tend to
be the same width in characters as yours anyway.

Of course, if you have boxed comments or code laid out in a tabular
form, they'll be screwed up by different tab sizes. Maybe you should
use spaces here, but unfortunately it's still easier to tab. There
ought to be one or two editors that'll convert tabs to spaces except for
the indentation tabs, but I don't know of one.

I personally tend to use tabs everywhere ITR, but if I'm going to
publish the code then I'd change the boxed comments to use spaces.

There are quite a few questions you can ask about indentation levels.
For example, how should switch/cases be indented? Similarly
class/private/protected/public? And of course, subsequent lines of a
statement. (My tendency here is to indent by two spaces, or six ("\t
") if it begins a block.)

Stewart.
 
M

Mike Wahler

Asfand Yar Qazi said:
Hi,

After much use of tabs worth 8 spaces, and constantly having to
format my C++ code in funny ways to keep all lines within an 80
character line length limit, I'm seriously thinking of switching
to using 4 spaces per tab. I feel like I'm about to lose a a
long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)

1. I never use tab characters in my source code, and
recommend that you don't either (different output
devices can/will interpret them differently).
I always use a sequence of space characters instead.

2. My personal style is to indent four spaces for each level.

3. Please don't post code with tabs in it here. Translate
them to the desired number of spaces first.

4. Many/most code editors have a facility to automatically
'convert tabs to spaces', so you can still have the convenience
of using the tab key, but without the problems inherent in having
actual tab characters in the code.


-Mike
 
J

Julie

Asfand said:
Hi,

After much use of tabs worth 8 spaces, and constantly having to
format my C++ code in funny ways to keep all lines within an 80
character line length limit, I'm seriously thinking of switching
to using 4 spaces per tab. I feel like I'm about to lose a a
long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)

Thanks,
Asfand Yar

If source code (constructs) were stored in a database rather that our current
ancient file-based formats, formatting, tabs/spaces, etc. would all become a
thing of the past. Regardless, that is a topic that isn't widely accepted nor
appropriate for this forum.

The best thing that you can do is to do away with your 'faithful companion',
and not seek another. Just get used to any format, and don't come up w/ one of
your own. I recommend regularly viewing other's code (of various styles) and
mimicking their style as practice, regardless of how ugly you consider it. It
won't be long and you will find yourself much more adept at examining and
understanding code, independent of the formatting, and this is a good thing.
 
C

Claudio Puviani

Julie said:
If source code (constructs) were stored in a database rather that our current
ancient file-based formats, formatting, tabs/spaces, etc. would all become a
thing of the past. Regardless, that is a topic that isn't widely accepted nor
appropriate for this forum.

You're getting really close to joining the Steve Hattons and the E. Robert
Tisdales in the killfiles of a lot of people by ceaselessly harping about your
personal pet theories. You've said your piece (dozens of times!), now let it die.

Claudio Puviani
 
S

Stephen Waits

Well, there's no reason you shouldn't set your tabs to be rendered at
whatever width you prefer in your editor. It's incredibly easy to make
code render perfectly on every editor regardless of tab size..

Here's how:

1. use TAB characters for indentation
2. use SPACE characters for alignment (always post-indentation!)

That's it.. follow those two rules and you'll produce highly renderable
code.


An example:

/**
* replace "->" with TAB characters
* replace "." with SPACE characters
**/
int main()
{
-> int..........x.=.10;....// x blah blah
-> unsigned.int.y.=.0;.....// y blah blah

-> printf(
-> ......."blah %d\n",
-> .......x
-> ......);

-> return 0;
}


And here it is again, but with actual tabs & spaces:

int main()
{
int x = 10; // x blah blah
unsigned int y = 0; // y blah blah

printf(
"blah %d\n",
x
);

return 0;
}

Does this example render perfectly for you? Then you get my point..


I lead a team who uses this strategy. We can all edit and view each
other's perfectly rendered code without hassle, and we all get to use
our preferred tab size. Some use 2, others 3, or 4.

--Steve
 
L

Leor Zolman

Actually, Mike, this post of yours dovetails so nicely with what I wanted
to say that I'll just use it as a template ;-)
1. I never use tab characters in my source code, and
recommend that you don't either (different output
devices can/will interpret them differently).
I always use a sequence of space characters instead.

I do use tabs in my personal source code, because that leads to the most
intuitive, busy-work-free editing sessions possible. I don't typically
share those source files with anyone, though. But when I do, see #3 below.
2. My personal style is to indent four spaces for each level.

Me too. I think it looks and works best, 80 columns or no 80 columns.
3. Please don't post code with tabs in it here. Translate
them to the desired number of spaces first.

I use Epsilon, and created a keystroke macro for myself that performs the
following steps:
1. tabifies the entire buffer (converts tabs to spaces)
2. copies the entire buffer into the clipboard
3. re-tabifies the entire buffer (puts back the tabs).
This allows me, with one keystroke combination (Alt-C is the one I chose),
to place the entire source file I'm editing into the clipboard, and then I
can just paste it into a newsgroup posting I'm working on.
4. Many/most code editors have a facility to automatically
'convert tabs to spaces', so you can still have the convenience
of using the tab key, but without the problems inherent in having
actual tab characters in the code.

Epsilon also has intelligent tabbing according to the extension of the file
you're editing. So regardless of whether you've got it set to use hard tabs
or just spaces, after you press Enter it'll automatically position the
cursor in the correct place on the next line. And you can tweak it to your
indentation style preferences (brace placement, etc.) as well. Quite cool.
-leor
 
J

Julie

Claudio said:
You're getting really close to joining the Steve Hattons and the E. Robert
Tisdales in the killfiles of a lot of people by ceaselessly harping about your
personal pet theories. You've said your piece (dozens of times!), now let it die.

I have absolutely _NO_ interest at all about being in anyone/everyone's
killfile. Feel free to put me in your killfile, ignore, or anything else w/ my
posts. I'm posting here for my benefit, and the benefit of others. If you
don't agree w/ me, or otherwise find my posts of interest, please ignore.

Claudio, please spend a little more time understanding what people post, and
allow them to voice their opinions. You don't happen to agree w/ a lot of what
I say, however, you are not in the position (self-imposed or otherwise) to pass
judgments on others or their ideas. I suspect that far fewer people are
interested in the contents of your killfile than my occasional (I think that I
have brought it up 3 separate times, including this one) commentary on my
'borwse-o-matic' [sic] as you call it. Further, whenever I have brought up my
thoughts, they have been topical and somewhat appropriate to the thread in
question.

Good bye.
 
E

E. Robert Tisdale

Jeff said:
Vim has helped a lot;
it has a concept of "shiftwidth" that makes the editor behave as
though tab were four spaces, but ^I actually inserts spaces instead of
tabs. Real tab stops are still set at eight-column intervals.

:set softtabstop=4
 
E

E. Robert Tisdale

Something said:
After much use of tabs worth 8 spaces
and constantly having to format my C++ code in funny ways
to keep all lines within an 80 character line length limit,
I'm seriously thinking of switching to using 4 spaces per tab.
I feel like I'm about to lose
a long suffering faithful companion...

What are your views on this? Any words of sympathy? :0)

This is an obvious troll.

Indentation is purely a style issue.
It is a private matter between you and God.

Modern text editors allow programmers to set tab stops where they wish.
You can use expand and unexpand to convert tabs to spaces
and spaces to tabs respectively.

You can use a C++ code reformatter like Artistic Style:

http://astyle.sourceforge.net/

to convert from one style to another and back again.
No, Julie, you don't need a special code data base to do this.

Any attempt to impose a particular style on another programmer
is megalomaniacal mischief.
 
A

Asfand Yar Qazi

E. Robert Tisdale said:
This is an obvious troll.

Indentation is purely a style issue.
It is a private matter between you and God.

Modern text editors allow programmers to set tab stops where they wish.
You can use expand and unexpand to convert tabs to spaces
and spaces to tabs respectively.

You can use a C++ code reformatter like Artistic Style:

http://astyle.sourceforge.net/

to convert from one style to another and back again.
No, Julie, you don't need a special code data base to do this.

Any attempt to impose a particular style on another programmer
is megalomaniacal mischief.

How can I be a troll for asking advice from experienced
programmers? Style is a very important issue in C++ programming,
and, if anything, I was asking experts to impose their style on me.
 
L

Leor Zolman

How can I be a troll for asking advice from experienced
programmers? Style is a very important issue in C++ programming,
and, if anything, I was asking experts to impose their style on me.

You have to learn to interpret around here. When E. Robert Tisdale says
"This is an obvious troll", that just means he disagrees with something
about the question, or suspects motivations other than the straight-forward
variety. Once in a blue moon it actually is a troll, but then it is a case
of the man who cried "Wolf!" anyway...

FWIW, I think tabbing policy is a serious issue worth tossing around once
in a while. I've certainly struggled with it and had to come up with some
policy decisions as I've moved from coding for myself only into dealing
with code intended for newsgroup postings. In the past I've had to deal
with it when preparing code for magazine publication, both as author of the
code and as a CUJ staffer dealing with lots of OPC (Other People's Code).
-leor
 
M

Mike Wahler

Asfand Yar Qazi said:
How can I be a troll for asking advice from experienced
programmers? Style is a very important issue in C++ programming,
and, if anything, I was asking experts to impose their style on me.

Asfand:

Do some reading of Mr. Tisdale's other posts in the archives of this group,
and then decide for yourself who is and is not a troll.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top