git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Documentation/CodingGuidelines: improve header includes rules

From
Christian Couder <chriscool@tuxfamily.org>
Date
Apr 16, 2009, 05:10 UTC
Message-ID
<200904160710.28473.chriscool@tuxfamily.org>
In-Reply-To
<20090415075819.GC23332@coredump.intra.peff.net>
Le mercredi 15 avril 2009, Jeff King a écrit :
Show 16 quoted lines
> On Tue, Apr 14, 2009 at 12:34:33AM +0200, Christian Couder wrote:
> > 	- The third one means that for example if we have "revision.h"
> > 	that includes "diff.h" and "commit.h", then it's ok to include
> > 	"revision.h" in a C file, only if at least one feature from
> > 	"revision.h" is actually used in the C file.
> >
> > 	It is not ok to include "revision.h" if features from "diff.h"
> > 	and "commit.h" are used but no feature from "revision.h" is
> > 	used.
>
> Why? I thought the guiding principle mentioned earlier was "don't waste
> programmers' time figuring out what should and shouldn't be included".
>
> Sure, I would not expect somebody to include a header that is totally
> unrelated, but it seems that most of the source files lazily include
> cache.h just to get "everything".
The third rule is:
+ - After the first #include in a C file, only header files containing
+   features that are actually used in the C file should be included.
+   (This means that it is not ok to include an header file only
+   because this header file includes other header files with features
+   that are used in the C file.)

So this rule is only for #include after the first one, and "cache.h" should be the first one if it is included.

I added this rule because we need a little sanity too and it can be misleading to see an include of some feature when in fact no features from the include are used.

> Stripping unnecessary includes doesn't even speed up compilation, as our
> Makefile overspecifies the header dependencies anyway.

Perhaps the dependencies will not be overspecified one day, and I think it should at least speed up a little bit compilation of the C file where unnecessary includes have been stripped. And anyway it looks simpler and does not mislead into thinking that some feature are used when they are not.

Best regards, Christian.

Previous: Jeff King
Message 4 of 4 in “Documentation/CodingGuidelines: improve header includes rules”
  1. Documentation/CodingGuidelines: improve header includes rulesChristian Couder, Apr 13, 2009
  2. Johannes SchindelinApr 14, 2009
  3. Jeff KingApr 15, 2009
  4. Christian CouderApr 16, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.