2007-11-08 10:37:10

by Benny Halevy

[permalink] [raw]
Subject: Coding Style: indenting with tabs vs. spaces

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


2007-11-08 16:02:52

by Chris Snook

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

Benny Halevy wrote:
> 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).

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

-- Chris

2007-11-10 12:14:17

by DervishD

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

Hi Benny :)

* Benny Halevy <[email protected]> dixit:
> 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

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

2007-11-10 12:36:21

by Xavier Bestel

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces


Le samedi 10 novembre 2007 ? 13:04 +0100, DervishD a ?crit :
> Hi Benny :)
>
> * Benny Halevy <[email protected]> dixit:
> > 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


2007-11-10 16:49:45

by DervishD

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

Bonjour Xavier :)

* Xavier Bestel <[email protected]> dixit:
> Le samedi 10 novembre 2007 à 13:04 +0100, DervishD a écrit :
> > * Benny Halevy <[email protected]> dixit:
> > > 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 ?

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

> 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

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

2007-11-11 08:57:59

by Benny Halevy

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

On Nov. 10, 2007, 14:27 +0200, Xavier Bestel <[email protected]> wrote:
> Le samedi 10 novembre 2007 ? 13:04 +0100, DervishD a ?crit :
>> Hi Benny :)
>>
>> * Benny Halevy <[email protected]> dixit:
>>> 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.

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.

>
> Xav
>
>

2007-11-11 09:33:53

by James Courtier-Dutton

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

DervishD wrote:
> Bonjour Xavier :)
>
> * Xavier Bestel <[email protected]> dixit:
>
>> Le samedi 10 novembre 2007 à 13:04 +0100, DervishD a écrit :
>>
>>> * Benny Halevy <[email protected]> dixit:
>>>
>>>> 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 ?
>>
>
> About the space saving? Not me, of course. It's just that I didn't see
> any other advantage.
>
>
>> 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
>
>

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


2007-11-12 08:13:33

by Benny Halevy

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

On Nov. 11, 2007, 11:23 +0200, James Courtier-Dutton <[email protected]> wrote:
> DervishD wrote:
>> Bonjour Xavier :)
>>
>> * Xavier Bestel <[email protected]> dixit:
>>
>>> Le samedi 10 novembre 2007 à 13:04 +0100, DervishD a écrit :
>>>
>>>> * Benny Halevy <[email protected]> dixit:
>>>>
>>>>> 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 ?
>>>
>> About the space saving? Not me, of course. It's just that I didn't see
>> any other advantage.
>>
>>
>>> 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
>>
>>
>
> 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

>
> James
>
>
> -

2007-11-12 08:18:19

by Benny Halevy

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

On Nov. 08, 2007, 17:58 +0200, Chris Snook <[email protected]> wrote:
> Benny Halevy wrote:
>> 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).
>
> 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

>
> -- Chris
> -

2007-11-12 15:38:22

by J. Bruce Fields

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

On Mon, Nov 12, 2007 at 10:13:08AM +0200, Benny Halevy wrote:
> 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.

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.

2007-11-12 16:29:14

by DervishD

[permalink] [raw]
Subject: Re: Coding Style: indenting with tabs vs. spaces

Hi Benny :)

* Benny Halevy <[email protected]> dixit:
> 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.

> 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

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