[Swan-dev] readable C style for split control statements

Paul Wouters paul at nohats.ca
Mon Oct 1 17:31:31 UTC 2018


On Mon, 1 Oct 2018, D. Hugh Redelmeier wrote:

> 	Outside of comments, documentation and except in Kconfig,
> 	spaces are never used for indentation, and the above example
> 	is deliberately broken.

I do change spaces I find into tabs as well.

> The obvious way to conform is to indent the second line one tab, as
> Tuomo has done.
>
> 		if (!NLMSG_OK(nlhdr, (size_t)readlen) ||
> 			nlhdr->nlmsg_type == NLMSG_ERROR)
> 			return -1;
>
> In the kernel code that I looked at, this was not commonly done.  So I
> think that this rule need not be taken too seriously.
>
> But there is a second, less awful, conforming possibility: indenting
> the second line with TWO tabs:
>
> 		if (!NLMSG_OK(nlhdr, (size_t)readlen) ||
> 				nlhdr->nlmsg_type == NLMSG_ERROR)
> 			return -1;

When these get too hard to read, especially with a 4 line if statement,
I tend to switch these to:

 		if (!NLMSG_OK(nlhdr, (size_t)readlen) ||
 			[...]
 			nlhdr->nlmsg_type == NLMSG_ERROR)
 		{
  			return -1;
 		}

> Oh, and another difference in my chosen style: when you break a line,
> you break it before the operation.  For things like IF, the second
> line is not further indented.  FreeS/Wan's Pluto used that style.
>
> 		if (!NLMSG_OK(nlhdr, (size_t)readlen)
> 		|| nlhdr->nlmsg_type == NLMSG_ERROR)
> 			return -1;
>
> At first glance, this looks odd and ugly.  The more you get used to it
> the more it makes sense.  The structure becomes completely evident
> from reading just the left side of text.  (Like all styles, it fails
> if the indentation doesn't actually match the braces etc.  And C
> compilers don't generally report when indentation is broken.)

I personally still prefer them at the end of the line :)

Paul


More information about the Swan-dev mailing list