Skip to contentSkip to conversation

Coding Style: indenting with tabs vs. spaces

DISCUSSION Benny Halevy 6 participants 11 messages Nov 8, 2007 – Nov 12, 2007

lore
Share & tools
lore
#0 lore raw

Greetings,

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

The gist of it is that tabs should be used for nesting, not for decoration. Indent your code with as many tabs as your nesting level, where all statements will begin, and from there on use space characters.
The rational behind it is to be tab-width agnostic so regardless of your tab expansion setup, the code will look correct and will make sense.

When you break a line and want the new line text to start below a specific point relative to the previous line (I consider that "decorating") then start the new line with the same number of tabs as the previous one and then just use space characters as their width is the same as any character in the previous line, (assuming fixed-width fonts of course).

For example:

{
	if (very_long_expression &&
	    it_needs_to_be_broken_into_several_lines)
		return a_very_long_result +
		       the_remainder_of_it_that_spilled_off +
		       to_the_next_lines;

	return printk("just my %d cents\n",
	              2);
}

Thanks,

Benny

#1 lore raw
Quoted 12 lines · Benny Halevy

Greetings,

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

The gist of it is that tabs should be used for nesting, not for decoration. Indent your code with as many tabs as your nesting level, where all statements will begin, and from there on use space characters.
The rational behind it is to be tab-width agnostic so regardless of your tab expansion setup, the code will look correct and will make sense.

Quoted 5 lines

When you break a line and want the new line text to start below a specific point relative to the previous line (I consider that "decorating") then start the new line with the same number of tabs as the previous one and then just use space characters as their width is the same as any character in the previous line, (assuming fixed-width fonts of course).

I find it meaningful to indent extended lines one extra tab stop, but beyond that I agree it is just decoration.

-- Chris

#2 lore raw
Quoted 13 lines · Chris Snook
Quoted 12 lines · Benny Halevy

Greetings,

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

The gist of it is that tabs should be used for nesting, not for decoration. Indent your code with as many tabs as your nesting level, where all statements will begin, and from there on use space characters.
The rational behind it is to be tab-width agnostic so regardless of your tab expansion setup, the code will look correct and will make sense.

Quoted 8 lines
Quoted 5 lines

When you break a line and want the new line text to start below a specific point relative to the previous line (I consider that "decorating") then start the new line with the same number of tabs as the previous one and then just use space characters as their width is the same as any character in the previous line, (assuming fixed-width fonts of course).

I find it meaningful to indent extended lines one extra tab stop, but beyond that I agree it is just decoration.

Yup, that's a valid convention, as long as there are no trailing spaces after that extra tab stop. Concatenating spaces to this one extra tab stop (as checkpatch allows for up to 7 spaces) for decoration works well just as long as everybody expand tabs the same way.

Benny

Quoted 3 lines

-- Chris
-

#3 lore raw
In reply to Benny Halevy

Hi Benny :)

* Benny Halevy <[email protected]> dixit:

Quoted 3 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

So my opinion is: use spaces. The code indentation will look the same on every editor no matter the tab settings.

Raúl Núñez de Arenas Coronado

Signature · 3 lines
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!
We are waiting for 13 Feb 2009 23:31:30 +0000 ...
#4 lore raw
Quoted 11 lines · DervishD

Hi Benny :)

* Benny Halevy <[email protected]> dixit:

Quoted 3 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

Who cares ?
The only advantage is that people can make tabs as big (or as small) as they wish. Tabs become "logical indentation". So one's indentation isn't forced on anotherone's editor.

Xav

#5 lore raw

Bonjour Xavier :)

* Xavier Bestel <[email protected]> dixit:

Quoted 6 lines · DervishD
Quoted 5 lines

* Benny Halevy <[email protected]> dixit:

Quoted 3 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

Quoted 6 lines
Quoted 4 lines

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

Who cares ?

About the space saving? Not me, of course. It's just that I didn't see any other advantage.

Quoted 3 lines

The only advantage is that people can make tabs as big (or as small) as they wish. Tabs become "logical indentation". So one's indentation isn't forced on anotherone's editor.

The only way of having a sane indentation using tabs is to make sure that ALL indentation are tabs, not a mix of tabs and spaces (spaces, if any, should be at the end of indentation for aesthetical purposes, but should be removed without the logical indentation being lost). A good editor can ensure that all indentation are tabs and not a mix, but a good editor can adapt indentation to your likings when loading the file and save the file translating your favourite indentation back to spaces or whatever.

If everybody used tabs correctly, indenting using tabs would be great, but IMHO indenting with spaces is much better.

Raúl Núñez de Arenas Coronado

Signature · 3 lines
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!
We are waiting for 13 Feb 2009 23:31:30 +0000 ...
#6 lore raw
Quoted 38 lines · DervishD

Bonjour Xavier :)

* Xavier Bestel <[email protected]> dixit:

Quoted 8 lines · DervishD
Quoted 7 lines

* Benny Halevy <[email protected]> dixit:

Quoted 4 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

Quoted 7 lines
Quoted 5 lines

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

Who cares ?

About the space saving? Not me, of course. It's just that I didn't see any other advantage.

Quoted 4 lines

The only advantage is that people can make tabs as big (or as small) as they wish. Tabs become "logical indentation". So one's indentation isn't forced on anotherone's editor.

The only way of having a sane indentation using tabs is to make sure that ALL indentation are tabs, not a mix of tabs and spaces (spaces, if any, should be at the end of indentation for aesthetical purposes, but should be removed without the logical indentation being lost). A good editor can ensure that all indentation are tabs and not a mix, but a good editor can adapt indentation to your likings when loading the file and save the file translating your favourite indentation back to spaces or whatever.

Quoted 6 lines

If everybody used tabs correctly, indenting using tabs would be great, but IMHO indenting with spaces is much better.

Raúl Núñez de Arenas Coronado

Just use the linux

./scripts/checkpatch.pl --file

It does all the indent checks for you before you submit a patch. I.e. I checks that one has not mixed tabs with spaces etc. So, any patches to the Linux kernel will have tabs used correctly. Where is the problem?

James

#7 lore raw
Quoted 44 lines · James Courtier-Dutton
Quoted 36 lines · DervishD

Bonjour Xavier :)

* Xavier Bestel <[email protected]> dixit:

Quoted 8 lines · DervishD
Quoted 7 lines

* Benny Halevy <[email protected]> dixit:

Quoted 4 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

Quoted 7 lines
Quoted 5 lines

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

Who cares ?

About the space saving? Not me, of course. It's just that I didn't see any other advantage.

Quoted 4 lines

The only advantage is that people can make tabs as big (or as small) as they wish. Tabs become "logical indentation". So one's indentation isn't forced on anotherone's editor.

The only way of having a sane indentation using tabs is to make sure that ALL indentation are tabs, not a mix of tabs and spaces (spaces, if any, should be at the end of indentation for aesthetical purposes, but should be removed without the logical indentation being lost). A good editor can ensure that all indentation are tabs and not a mix, but a good editor can adapt indentation to your likings when loading the file and save the file translating your favourite indentation back to spaces or whatever.

Quoted 6 lines

If everybody used tabs correctly, indenting using tabs would be great, but IMHO indenting with spaces is much better.

Raúl Núñez de Arenas Coronado

Quoted 8 lines

Just use the linux

./scripts/checkpatch.pl --file

It does all the indent checks for you before you submit a patch. I.e. I checks that one has not mixed tabs with spaces etc. So, any patches to the Linux kernel will have tabs used correctly. Where is the problem?

checkpatch allows to indent with any number of tabs and up to 7 spaces. This is consistent with Documentation/CodingStyle and therefore can be considered "correct". However, forcing everybody to the same tab expansion setup is too limiting, especially when working in several environments at the same time where some of them may not be the linux kernel.

Using only spaces as DervishD suggested works around that using brute force by forcing the user to the author's preference which is legitimate but may not be the most productive way.

I think that my proposal of using tabs as logical indents only (as Xav put it) and spaces for decorative alignment provides the best of both worlds. One can expand the tabs to any number of spaces as one likes and then the trailing spaces will work on any editor setup as long as you use fixed-width font. That's not considered "correct" as per checkpatch but works much better for me.

Benny

Quoted 5 lines

James

-

#8 lore raw
Quoted 10 lines · Benny Halevy

checkpatch allows to indent with any number of tabs and up to 7 spaces. This is consistent with Documentation/CodingStyle and therefore can be considered "correct". However, forcing everybody to the same tab expansion setup is too limiting, especially when working in several environments at the same time where some of them may not be the linux kernel.

Using only spaces as DervishD suggested works around that using brute force by forcing the user to the author's preference which is legitimate but may not be the most productive way.

Quoted 6 lines

I think that my proposal of using tabs as logical indents only (as Xav put it) and spaces for decorative alignment provides the best of both worlds. One can expand the tabs to any number of spaces as one likes and then the trailing spaces will work on any editor setup as long as you use fixed-width font. That's not considered "correct" as per checkpatch but works much better for me.

The tabs-for-indent, spaces-for-alignment policy been proposed before; this:

	http://marc.info/?t=119251728100004&r=1&w=2

was only on the git mailing list, but we could probably dig up similar discussions on lkml with some more work.

Speaking as someone who doesn't care, but doesn't see it as a particularly interesting discussion, either....

--b.

#9 lore raw
In reply to Benny Halevy

Hi Benny :)

* Benny Halevy <[email protected]> dixit:

Quoted 3 lines

Using only spaces as DervishD suggested works around that using brute force by forcing the user to the author's preference which is legitimate but may not be the most productive way.

I admit it.

Quoted 3 lines

I think that my proposal of using tabs as logical indents only (as Xav put it) and spaces for decorative alignment provides the best of both worlds.

Yes, you're right, and it is the only "mix" of tabs and spaces that won't cause headaches. I still don't like tabs for indenting but that's a matter of personal taste. Sometimes I use tabs for indenting when I have to share code.

Raúl Núñez de Arenas Coronado

Signature · 3 lines
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!
We are waiting for 13 Feb 2009 23:31:30 +0000 ...
#10 lore raw
In reply to Xavier Bestel
Quoted 12 lines · Xavier Bestel
Quoted 10 lines · DervishD

Hi Benny :)

* Benny Halevy <[email protected]> dixit:

Quoted 3 lines

I would like to hear peoples opinion about the indentation convention described below that I personally found the most practical with several different editors.

While I respect you opinion about tabs, I find tab indentation the most evil thing ever invented. Even if done right (that is, not indenting using a mixture of spaces and tabs), the only advantage is that you save a few bytes.

Quoted 4 lines

Who cares ?
The only advantage is that people can make tabs as big (or as small) as they wish. Tabs become "logical indentation". So one's indentation isn't forced on anotherone's editor.

Right. That's exactly the point.
I find it harder to read someone else's code if (s)he uses 2 space indentation. With tabs, when done right, I can expand to my personal preference.

Quoted 4 lines

Xav