Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Header File Clutter

Reply
Thread Tools

Re: Header File Clutter

 
 
Ian Collins
Guest
Posts: n/a
 
      01-13-2011
On 01/14/11 08:33 AM, Joe Hesse wrote:
> When I write many X.h and X.cpp files and debug/modify them, after a
> while they contain library or other header files that may not be
> necessary. I realize that there are #ifdef's that prevent a header file
> from being included more than once so it doesn't hurt to put them in more
> places than necessary.
>
> My question is: Is there a tool to take a C++ program that compiles and
> reorganize the header files so there are no more #include<blah> than
> necessary?


While it is possible, what real benefits would such a tool bring?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Keith H Duggar
Guest
Posts: n/a
 
      01-13-2011
On Jan 13, 3:11*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 01/14/11 08:33 AM, Joe Hesse wrote:
>
> > When I write many X.h and X.cpp files and debug/modify them, after a
> > while they contain library or other header files that may not be
> > necessary. *I realize that there are #ifdef's that prevent a header file
> > from being included more than once so it doesn't hurt to put them in more
> > places than necessary.

>
> > My question is: *Is there a tool to take a C++ program that compiles and
> > reorganize the header files so there are no more #include<blah> *than
> > necessary?

>
> While it is possible, what real benefits would such a tool bring?


I would have thought it obvious that it can significantly reduce
compile times and especially eliminate dependency triggers.

KHD
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-13-2011
On Thu, 2011-01-13, Ian Collins wrote:
> On 01/14/11 08:33 AM, Joe Hesse wrote:
>> When I write many X.h and X.cpp files and debug/modify them, after a
>> while they contain library or other header files that may not be
>> necessary. I realize that there are #ifdef's that prevent a header file
>> from being included more than once so it doesn't hurt to put them in more
>> places than necessary.
>>
>> My question is: Is there a tool to take a C++ program that compiles and
>> reorganize the header files so there are no more #include<blah> than
>> necessary?

>
> While it is possible, what real benefits would such a tool bring?


Reducing compile times (sometimes drastically).
Improving readability -- if a file starts with '#include <foolib.h>'
you expect it to use functionality from foolib.

Yet, I don't think automatic "weeding" is desireable. The list of
#includes is to some extent documentation, and should be maintained
by a real person, IMHO.

I personally do some manual weeding now and then but don't spend a
lot of time trying to get it exactly right.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-13-2011
On 01/14/11 09:25 AM, Keith H Duggar wrote:
> On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 01/14/11 08:33 AM, Joe Hesse wrote:
>>
>>> When I write many X.h and X.cpp files and debug/modify them, after a
>>> while they contain library or other header files that may not be
>>> necessary. I realize that there are #ifdef's that prevent a header file
>>> from being included more than once so it doesn't hurt to put them in more
>>> places than necessary.

>>
>>> My question is: Is there a tool to take a C++ program that compiles and
>>> reorganize the header files so there are no more #include<blah> than
>>> necessary?

>>
>> While it is possible, what real benefits would such a tool bring?

>
> I would have thought it obvious that it can significantly reduce
> compile times and especially eliminate dependency triggers.


Eliminate dependency triggers, yes but significantly reduce compile
times? Include guards take care of most waste there.

--
Ian Collins
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      01-13-2011
On Jan 13, 1:03*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 01/14/11 09:25 AM, Keith H Duggar wrote:
>
>
>
> > On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> *wrote:
> >> On 01/14/11 08:33 AM, Joe Hesse wrote:

>
> >>> When I write many X.h and X.cpp files and debug/modify them, after a
> >>> while they contain library or other header files that may not be
> >>> necessary. *I realize that there are #ifdef's that prevent a header file
> >>> from being included more than once so it doesn't hurt to put them in more
> >>> places than necessary.

>
> >>> My question is: *Is there a tool to take a C++ program that compiles and
> >>> reorganize the header files so there are no more #include<blah> * *than
> >>> necessary?

>
> >> While it is possible, what real benefits would such a tool bring?

>
> > I would have thought it obvious that it can significantly reduce
> > compile times and especially eliminate dependency triggers.

>
> Eliminate dependency triggers, yes but significantly reduce compile
> times? *Include guards take care of most waste there.


I wouldn't be so sure. I've heard that judicious use of pImpl combined
with removing superfluous headers can dramatically reduce build times,
like on the order of 10x or more.

I've had some confirming anecdotal evidence as well while working on a
codebase of ~4000 cpp files.
 
Reply With Quote
 
Nelson Ferreira
Guest
Posts: n/a
 
      01-13-2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 1/13/2011 3:11 PM, Ian Collins wrote:
> On 01/14/11 08:33 AM, Joe Hesse wrote:
>> When I write many X.h and X.cpp files and debug/modify them, after a
>> while they contain library or other header files that may not be
>> necessary. I realize that there are #ifdef's that prevent a header file
>> from being included more than once so it doesn't hurt to put them in more
>> places than necessary.
>>
>> My question is: Is there a tool to take a C++ program that compiles and
>> reorganize the header files so there are no more #include<blah> than
>> necessary?

>
> While it is possible, what real benefits would such a tool bring?
>


The commercial tool Coverity has a checker for some of that.
It does not reorganize to the optimum, but it will tell you when you
are including a header unnecessarily.

- --
Nelson Ferreira
Pubkey: http://triatlantico.org/njsf.asc
(9DFE 82A7 C689 BAEE EC25 A8E8 3C64 1789 7420 ED20)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0vas4ACgkQPGQXiXQg7SBEsACfbEQi9OIF/9wj0bVXmaAZS2lC
m+oAnihfzYga4tzvTtnt0zAR9GhX3Jf9
=7j9H
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-13-2011
On 01/14/11 10:06 AM, Joshua Maurice wrote:
> On Jan 13, 1:03 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 01/14/11 09:25 AM, Keith H Duggar wrote:
>>
>>
>>
>>> On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>>>> On 01/14/11 08:33 AM, Joe Hesse wrote:

>>
>>>>> When I write many X.h and X.cpp files and debug/modify them, after a
>>>>> while they contain library or other header files that may not be
>>>>> necessary. I realize that there are #ifdef's that prevent a header file
>>>>> from being included more than once so it doesn't hurt to put them in more
>>>>> places than necessary.

>>
>>>>> My question is: Is there a tool to take a C++ program that compiles and
>>>>> reorganize the header files so there are no more #include<blah> than
>>>>> necessary?

>>
>>>> While it is possible, what real benefits would such a tool bring?

>>
>>> I would have thought it obvious that it can significantly reduce
>>> compile times and especially eliminate dependency triggers.

>>
>> Eliminate dependency triggers, yes but significantly reduce compile
>> times? Include guards take care of most waste there.

>
> I wouldn't be so sure. I've heard that judicious use of pImpl combined
> with removing superfluous headers can dramatically reduce build times,
> like on the order of 10x or more.
>
> I've had some confirming anecdotal evidence as well while working on a
> codebase of ~4000 cpp files.


I guess a lot depends on where you are starting from. If build time is
considered early enough in a project, it is less of an issue.

--
Ian Collins
 
Reply With Quote
 
Keith H Duggar
Guest
Posts: n/a
 
      01-13-2011
On Jan 13, 4:03*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 01/14/11 09:25 AM, Keith H Duggar wrote:
> > On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> *wrote:
> >> On 01/14/11 08:33 AM, Joe Hesse wrote:
> >>> When I write many X.h and X.cpp files and debug/modify them, after a
> >>> while they contain library or other header files that may not be
> >>> necessary. *I realize that there are #ifdef's that prevent a header file
> >>> from being included more than once so it doesn't hurt to put them in more
> >>> places than necessary.

>
> >>> My question is: *Is there a tool to take a C++ program that compiles and
> >>> reorganize the header files so there are no more #include<blah> * *than
> >>> necessary?

>
> >> While it is possible, what real benefits would such a tool bring?

>
> > I would have thought it obvious that it can significantly reduce
> > compile times and especially eliminate dependency triggers.

>
> Eliminate dependency triggers, yes but significantly reduce compile
> times? *Include guards take care of most waste there.


Unfortunately they do not (unless you are talking about external
guards?). The reason is that due to the textual include mechanism
and C++ language rules, internal guards still require that the
entire contents of the header file and recursively on down must
be scanned and tokenized (but not parsed etc).

And, it turns out, that for C++ code scanning and tokenization can
be a substantial if not the lion's share of total compilation time.
Especially with "modern" code where the headers often comprise huge
portions of the code.

By the way, Walter Bright (as you know a genius compiler writer)
has IIRC talked discussed this a few times in this forum usually
in the context of a "true" modules system and the inadequacy of
pre-compiled headers.

KHD
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      01-13-2011
On Thu, 2011-01-13, Ian Collins wrote:
> On 01/14/11 09:25 AM, Keith H Duggar wrote:
>> On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>>> On 01/14/11 08:33 AM, Joe Hesse wrote:
>>>
>>>> When I write many X.h and X.cpp files and debug/modify them, after a
>>>> while they contain library or other header files that may not be
>>>> necessary. I realize that there are #ifdef's that prevent a header file
>>>> from being included more than once so it doesn't hurt to put them in more
>>>> places than necessary.
>>>
>>>> My question is: Is there a tool to take a C++ program that compiles and
>>>> reorganize the header files so there are no more #include<blah> than
>>>> necessary?
>>>
>>> While it is possible, what real benefits would such a tool bring?

>>
>> I would have thought it obvious that it can significantly reduce
>> compile times and especially eliminate dependency triggers.

>
> Eliminate dependency triggers, yes but significantly reduce compile
> times? Include guards take care of most waste there.


But eliminating dependency triggers is a way to reduce compile times!

If I edit foo.h, type "make" and a dozen files are recompiled even
though the change in foo.h cannot affect the twelve object files, that
time is wasted.

(Not that I think a tool is the answer; see elsewhere in the thread.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Kevin P. Fleming
Guest
Posts: n/a
 
      01-13-2011
On 01/13/2011 03:47 PM, Keith H Duggar wrote:
> On Jan 13, 4:03 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>> On 01/14/11 09:25 AM, Keith H Duggar wrote:
>>> On Jan 13, 3:11 pm, Ian Collins<ian-n...@hotmail.com> wrote:
>>>> On 01/14/11 08:33 AM, Joe Hesse wrote:
>>>>> When I write many X.h and X.cpp files and debug/modify them, after a
>>>>> while they contain library or other header files that may not be
>>>>> necessary. I realize that there are #ifdef's that prevent a header file
>>>>> from being included more than once so it doesn't hurt to put them in more
>>>>> places than necessary.

>>
>>>>> My question is: Is there a tool to take a C++ program that compiles and
>>>>> reorganize the header files so there are no more #include<blah> than
>>>>> necessary?

>>
>>>> While it is possible, what real benefits would such a tool bring?

>>
>>> I would have thought it obvious that it can significantly reduce
>>> compile times and especially eliminate dependency triggers.

>>
>> Eliminate dependency triggers, yes but significantly reduce compile
>> times? Include guards take care of most waste there.

>
> Unfortunately they do not (unless you are talking about external
> guards?). The reason is that due to the textual include mechanism
> and C++ language rules, internal guards still require that the
> entire contents of the header file and recursively on down must
> be scanned and tokenized (but not parsed etc).


Using "#pragma once" as the first line of the header file solves this
problem, and is supported in nearly every modern version of GCC, Visual
Studio and any compiler that uses MCPP as its preprocessor.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AD Controlling Creative Clutter" Sale Ends Tomorrow Herbie Digital Photography 0 01-13-2008 05:09 PM
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
10 Tips on Computer Clutter Clean Up OurCompGuy Computer Information 1 01-04-2007 02:21 PM
Cookie and clutter clean up Ron P Firefox 7 04-13-2006 02:00 AM
clutter on my hard drive...help!!.. Zippo Computer Support 9 12-10-2004 08:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57