[PATCH 1 of 6] C modules: don't mix tabs and spaces

Matt Mackall mpm at selenic.com
Tue Sep 1 00:23:06 UTC 2009


On Thu, 2009-08-27 at 16:04 +0200, Nicolas Dumazet wrote:
> 2009/8/27 Maxim Dounin <mdounin at mdounin.ru>:
> >> Use tabs seems it is apparently the standard.
> >
> > I believe it's linux kernel style which is used for C code.  Matt
> > wrote about it some time ago.
> 
> I probably haven't been around for long enough ;)
> 
> > [...]
> >
> >> @@ -253,7 +253,7 @@
> >>       pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos));
> >>       /* we can't have more matches than lines in the shorter file */
> >>       l.head = l.base = (struct hunk *)malloc(sizeof(struct hunk) *
> >> -                                             ((an<bn ? an:bn) + 1));
> >> +                                                                                     ((an<bn ? an:bn) + 1));
> >
> > Here is a good illustration why tabs shouldn't be used to pad
> > continuation lines (are you using tabwidth=4 in your editor? try
> > default - 8).  Even if tabs used for indenting.
> 
> Yuck. Looks ugly indeed.
> 
> I think it's a good illustration of why we should avoid at all cost
> mixing tabs and
> spaces. Either pick all-spaces, either pick all-tabs.
> Using spaces for continuation lines is not the magic solution: if the
> previous line is indented using tabs,
> then the overall render still depends on editor-specific
> tabwidth/tabexpand value
> 
> But I'm young and innocent. Don't feed me to the trolls -- yet :)

Here are my rules for tabs and spaces in C:

1) never use tabs after initial non-whitespace

#define FOO    4
#define QUUX   5 

If you use tabs to put things in a column like this, tabstop changes =
fail. Generally, you shouldn't be putting such things in columns, but
sometimes it's worth the trouble. So use spaces.

2) use tabs for block-level indentation (duh)

if (a) {
	b();
	c();
}


3) use either tabs only or leading tabs + padding spaces for line
continuations

	if (a &&
	    b) {  /* 4 spaces lineup immediately under 'a' */
		c();
		d();
	}

Note that the tabs come first. Thus, if tab width changes then a and b
continue to line up. Tabs and spaces are often preferable for complex
continuations - just like normal indentation, they help indicate nesting
levels in complex expressions. 

4) tabs are really 8 spaces, so if you break any of these rules while
using a nonstandard setting, you deserve an extra helping of shame

-- 
http://selenic.com : development and support for Mercurial and Linux




More information about the Mercurial-devel mailing list