    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Include Files</title>
        <link>https://members.accu.org/index.php/articles/476</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #39 - Sep 2000</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c165/">39</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-165/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+165/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Include Files</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 September 2000 17:50:58 +01:00 or Tue, 26 September 2000 17:50:58 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id=
"d0e18"></a>Introduction</h2>
</div>
<p>Although most of us work with the C/C++ <tt class=
"literal">#include</tt> mechanism every working day it is an area
that has received surprisingly little coverage and analysis. To
date I have regarded John Lakos's <i class="citetitle">Large Scale
C++ Design</i> as the most authoritative work on the subject.
Kevlin Henney's piece on <i class="citetitle">Source Cohesion and
Decoupling</i> provides a solid basis for understanding the theory
involved with include files.</p>
<p>For my part I'd like to share some of my rules-of-thumb. I've
adopted these over the last few years, heavily influenced by Lakos,
although this is my first attempt at codifying them as such. Having
read Kevlin's piece I have a few more ideas to try out. My emphasis
tends to be on simplifying the work-a-day processes, improving
re-usability and portability. For the most part I agree with Lakos
and Henney - although not always!</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e34" id="d0e34"></a>Primitive
mechanism</h2>
</div>
<p>Perhaps one reason by <tt class="literal">#include</tt> has not
received much analysis is because it is so primitive, other
languages have similar constructs but few are as primitive.
<tt class="literal">#include &quot;widget.h&quot;</tt> literally says &quot;open
the file <tt class="filename">widget.h</tt> and insert the code
here as if it where part of this physical file.&quot; No syntax checking
is provided, just about the only check that is provided is that the
file <tt class="filename">widget.h</tt> exists, most compiler will
get a little upset if it doesn't. It's often worth remembering just
how primitive this mechanism is.</p>
<p>We could, for example, actually include the .cpp file into our
file, this would remove the need to link in another object file -
to the compilers its all, one big compilation unit. This technique
can occasionally be useful but on the whole it's not advocated as a
method of working. Instead, we use an include to simply declare
prototypes (for functions), class declarations and other such
non-procedural items. The actual source code for these is elsewhere
and it is the linkers job to match it all up.</p>
<p>For all it's primitive-ness, <tt class="literal">#include</tt>
is the basis of code reuse: at the simplest level, we have a set of
common functions for which we provide prototypes in a .h
file<sup>[<a name="d0e58" href="#ftn.d0e58" id=
"d0e58">1</a>]</sup>. After including the file the functions are
available for us. We may choose to compile the functions separately
and package them as a library.</p>
<p>Here we see another benefit of this approach: we are
implementing abstraction, we can no longer see the source for a
function only it's prototype. This breaks down very quickly, as
often prototypes are not commented with the information we are
looking for and we end up finding the source code and reading
that.</p>
<p>At the next level, we implement class definitions in the header
file and place the implementation in the hidden file. Now we are
implementing data abstraction, or data hiding; although data hiding
is one of the strong points of object oriented design, it's not the
full story.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e66" id="d0e66"></a>Rules of
Inclusion: Basic rules</h2>
</div>
<p><span class="bold"><b>Rule 1:</b></span> Include everything you
need in a file, no more, no less</p>
<p>When we re-use code at either the function, class or component
level we are trying to save time and effort. If code is to be
re-usable it must be self contained and presented in such a way
that it can be used in our projects with minimal effort.</p>
<p>This rule means: include all the header files you need to ensure
this header file will compile but don't include any more.</p>
<pre class="programlisting">
#include &lt;string&gt;
class Address;  // forward declaration
class Person {
  Person(const std::string&amp; name, const Address&amp; addr);
  ...
};
</pre>
<p>In this example, string is included because it is needed, if
string is not declared before the compiler encounters this code it
will error. Don't say &quot;well the file that includes this file
already includes string, so I don't need it here&quot; : if you decide
to reuse Person on the next project you don't have this guarantee,
you'll either need to change this file, which you don't want to do
because the code is shared between two projects now, hence you will
have source control issues, or, the new file that includes this one
will have to include string, which itself violates this rule.</p>
<p>The example does not include <tt class=
"filename">string_util.h</tt>, <tt class=
"filename">strstream.h</tt>, or anything else, it doesn't need
them. If some other file in the system needs one of these the
proper place to include it is in that file. Including stuff you
don't need immediately in this file will (a) slow down the compile,
(b) pollute the namespace and (c) introduce dependencies which may
cause problems later.</p>
<p>This rule is basically a re-phrasing of Henney's <span class=
"emphasis"><em>Minimal Header</em></span> and <span class=
"emphasis"><em>Cohesive Header</em></span>.</p>
<p>Before moving on there is a school of thought which says: only
include what you absolutely need, if someone else has already
included it don't bother. This can reduce compile times, but it
detracts from re-usability. I've heard, and seen, this approach
taken but never seen a well argued case for it. The main case I can
see is to reduce compile times.</p>
<p><span class="bold"><b>Rule 2:</b></span> Use forward
declarations wherever possible and practical</p>
<p>We could go further with the example above. Because string is
not actually declared but only referenced, we could remove the
include altogether and just present a forward declaration of string
as I have done with Address.</p>
<p>If this where a Microsoft CString, I'd probably of done just
that. But because string is in namespace std I'd need to open the
namespace, this would probably double or even treble the number of
lines concerned, then, because string is actually a typedef for
basic_string, I'd have to work around this too, then basic_string
is actually a template so more work. As the code stands it is
obvious what is happening, if I where to add a full forward
declaration of string the actual code would be obscured by the
declaration, hence I don't consider it practical. In addition, it's
probably that any use of this class is going to include string
anyway (I know, I said don't rely on this) so I'm more concerned
with clarity of code.</p>
<p>However, I could provide a forward declaration in another file
and include that - Henney's <span class="emphasis"><em>Forward
Declaring Header</em></span>. I'm happy to agree I should, I'm also
happy not to get hung up on the issue. Balance the options, on a
large project it is probably worth it, then once you have the
forward reference file it there for small projects too. However, on
a small project you have to balance this against the fact that you
have added another file to the project which has to be managed and
maintained, and potentially shared across projects.</p>
<p><span class="bold"><b>Rule 3:</b></span> Use references wherever
possible</p>
<p>This also leads to a more general point, because I can use
forward declarations with pointers and references , but I not with
actual instantiations of classes I prefer to use reference in my
code where possible. This is also good advice because it cuts down
on the number of copy constructors being called - although remember
to accept parameters as const-references.</p>
<p><span class="bold"><b>Rule 4:</b></span> Be consistent with your
filename case - preferably always use lower-case</p>
<p>Is <tt class="literal">#include &lt;Windows.h&gt;</tt> the same
as <tt class="literal">#include &lt;windows.h&gt;</tt>? On
Microsoft systems, yes, on UNIX, no.</p>
<p>It is very easy on Windows systems to forget that filename case
can be important. If your using Samba to share files things can get
more complex as, depending on configuration, it can change the case
of a filename. By far the simplest solution is to mandate the
problem away. (I suggest lower-case over uppercase as most of
standard header files are all lower case, and, I think it looks
better.)</p>
<p>Sometimes it's beneficial to adopt conventions because, even if
they aren't the best, it's what people expect. Going against a
convention can cause a lot of convention - the rule of &quot;least
astonishment.&quot;</p>
<p><span class="bold"><b>Rule 5:</b></span> Only have one copy of
the file on your system</p>
<p>There should be no need in a build environment to have two
copies of one file. This is unnecessary duplication. It also leads
to the &quot;Which file was changed?&quot; scenario. You may think instance A
is being included and change this one, but in reality it's instance
B, so you keep getting the same error!</p>
<p>( In truth, modern compilers and IDEs are better at describing
exactly which file has changed so this problem arises less often
than it did.)</p>
<p>This leads to.....</p>
<p><span class="bold"><b>Rule 6:</b></span> File names should be
unique</p>
<p>Even where files are separated in different sub-directories it
makes sense to keep the actual file names unique. Now we are no
longer restricted to 8.3 filenames there is little reason to re-use
a name.</p>
<p>Keeping filenames unique will not only rule out any chance that
the compiler will include the wrong file but will also make your
code more self documenting. After all, we are only human, if we see
a file called <tt class="filename">StUtils.h</tt> we will first
assume it is the same <tt class="filename">StUtils.h</tt> (for
strings) that we have already seen, not some other <tt class=
"filename">StUtils.h</tt> (for streams) which is included because
of magic in the make file.</p>
<p>( This is one of the reasons I don't like Microsoft's
pre-compiled header system which places multiple files called
<tt class="filename">stdafx.h</tt> all over my project.)</p>
<p><span class="bold"><b>Rule 7:</b></span> only <tt class=
"literal">#include</tt> files at the top of your program</p>
<p>You can include files anywhere you like, remember, the compiler
doesn't syntax check include files. However, with the exception of
machine generated code, nobody expects to find:</p>
<pre class="programlisting">
// file: bar.cpp
vector&lt;int&gt;  wont_compile;
... loads of code ...
#include &lt;vector&gt;
... some more code ...
vector&lt;char&gt;  will_compile;
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e177" id="d0e177"></a>Rules of
Inclusion: Include guards</h2>
</div>
<p>The items listed here fall under Henney's <span class=
"emphasis"><em>Indempotent Header</em></span> pattern.</p>
<p><span class="bold"><b>Rule 8:</b></span> Guard the extremes of
your file</p>
<p>Few C/C++ programmers don't place include guards around their
header files:</p>
<pre class="programlisting">
#if !defined(_WIDGET_)
#define _WIDGET_
....
#endif
</pre>
<p>This is only sensible as we wish to avoid errors and warnings
when a file is included for the second time. But even here there is
room for differences. Some prefer the <tt class="literal">#if</tt>
to come before the comments and some prefer it after the
comments.</p>
<p>I don't see any point in placing the guards anywhere but on the
first line and last line of the file. It may make little difference
to the compiler, but the <tt class="literal">#if</tt><tt class=
"literal">...</tt><tt class="literal">#endif</tt> has nothing to do
with the source code in the file, the statements only exist because
of deficiencies in C so keep them away from the guts.</p>
<p><span class="bold"><b>Rule 9:</b></span> Think about include
guards, especially for libraries</p>
<p>Lakos argues that we should actually place the check in the file
which includes the header file, hence including the above would
look like:</p>
<pre class="programlisting">
...
#if !defined(_WIDGET_)
#include &lt;Widget.h&gt;
#endif
</pre>
<p>Lakos makes a strong case for this. Simply put: even with guards
within the file the compiler will still scan it looking for the
end. This may be a trivial amount of time, but on a large project,
with lots of files it mounts up and can effect compiler times.</p>
<p>Opposed to this, it makes the code more difficult to read and
unusual. Both are legitimate points of view and I advise you to
think about them.</p>
<p>(If your reaction to the above is to think &quot;I could right a
macro to remove the <tt class="literal">#if</tt>&quot; think again,
because this <span class="bold"><b>is</b></span> the preprocessor
you can't!)</p>
<p>On a library this advice goes twice, as you don't know how
people will include your files you should cater for the worst case
and, the potential speed improvement is multiplied across all
projects which use the library.</p>
<p><span class="bold"><b>Rule 10:</b></span> Keep internal
guards</p>
<p>If you adopt the &quot;external guard&quot; rule you may also consider
removing the internal guards. If you can guarantee all the files in
your project will follow it you may be safe. But, there are two
problems: new programmer will instinctively wonder where the
include guards are and secondly your code will be less re-usable in
systems which use &quot;internal guards.&quot;</p>
<p><span class="bold"><b>Rule 11:</b></span> Don't rely on
<tt class="literal">#pragma once</tt> without thinking.</p>
<p>Microsoft provide the <tt class="literal">#pragma once</tt> to
prevent header files being included more than once - I'm not aware
of any other vendor who also supports this, but that doesn't mean
there aren't any. While this is a useful directive it has obvious
cross platform implications and should not be used without
consideration. If you do decide to us it you may wish to use
regular include guards as well; while these would simplify porting
you may still see warnings on other platforms.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e247" id="d0e247"></a>Rules of
Inclusion: Include ordering</h2>
</div>
<p><span class="bold"><b>Rule 12:</b></span> Organise the include
order to recognise different type of files being included</p>
<p>The include statements are usually the first thing of
significance we see in a file, in that way they help shape our
expectations for what we are about to see - another good reason for
including exactly what the files will need.</p>
<p>We could throw down our includes in what-ever random order we
feel like, or the order we think we happen to need the files. I
think there is good reason to always order the includes in a
consistent fashion.</p>
<p>There are, to my mind, three classes of files we are including:
system includes (those for the platform and standard libraries,
e.g. <tt class="filename">stdio[.h]</tt>, <tt class=
"filename">vector[.h]</tt>, <tt class="filename">windows.h</tt>,
<tt class="filename">socket.h</tt>, etc.); project specific files
(usually libraries or objects that have been developed for this
project, or are reusable within this organisation); and finally
files specific to this file (e.g. the declaration of the class
implemented in this file, or prototypes for functions implemented
here.)</p>
<p>These categories are not water tight:</p>
<p>&quot;where does one include a third party library such as Rogue
Wave?&quot; : I normally put it in the second</p>
<p>&quot;where does one include the header file of the parent class I'm
inheriting from?&quot; : I normally put this in the third group
(although it need only needs to be included in the header file
declaring this class, because the implementation must include
declaration header.)</p>
<p>Each of these three groups should be grouped together.</p>
<p><span class="bold"><b>Rule 13:</b></span> Include system files,
then project files and finally local includes</p>
<p>For example:</p>
<pre class="programlisting">
// file: Employee.cpp
// system includes 
#include &lt;windows.h&gt;
#include &lt;vector&gt;
// project includes 
#include &lt;string_utils.h&gt;
#include &lt;hr_dept_component.h&gt;
#include &lt;Address.h&gt;
// local includes 
#include &quot;Employee.h&quot;
</pre>
<p>There are a few points that need to be observed here:</p>
<p>These rules apply to both the <tt class="filename">.h</tt> files
and the <tt class="filename">.cpp</tt> files: since the
declarations (<tt class="filename">.h</tt>'s) must be included in
the implementations (<tt class="filename">.cpp</tt>'s) for them to
compile a group of includes are already given. These are normally
the more visible dependencies, while the includes in the <tt class=
"filename">.cpp</tt> have only to do with implementation.</p>
<p>It is quiet common for sections not to be used, if so comment
then, e.g. <tt class="literal">// &lt;none&gt;</tt> this says a
lot</p>
<p>Attentive readers will of noticed that I advocate the opposite
of Lakos and Henney: both suggest placing the local or project
includes first and the system includes later. I would argue that my
ordering reduces work when a name clash is encountered. For
example, suppose <tt class="filename">hr_dept_component.h</tt>
defined a function to fire-people called <tt class=
"function">FreeResource</tt> (sorry, I can't think of a better
example at the moment!). This may work fine on the UNIX port but on
the NT version it suddenly conflicts with a Microsoft function from
<tt class="filename">Windows.h</tt>.</p>
<p>If we include <tt class="filename">hr_dept_component.h</tt>
before <tt class="filename">Windows.h</tt> the compiler will
generate an error on the line in <tt class=
"filename">Windows.h</tt>. Unfortunately, there is very little we
can do: we can't change <tt class="filename">Windows.h</tt>, it
works fine in every other program, it is us who have introduced the
problem; the error tells us nothing about where the conflict was
introduced in our own code.</p>
<p>However, if we include <tt class="filename">Windows.h</tt>
before <tt class="filename">hr_dept_component.h</tt> the compiler
will flag an error in <tt class="filename">hr_dept_component.h</tt>
where we may be able to do something.</p>
<p>The general rule is to put the least changeable includes first:
generally you do not want to change a system level file; project
level files can be changed but we'd rather not (it may involve
talking to another programmer!), but changing local includes, we
can do that now.</p>
<p><span class="bold"><b>Rule 14:</b></span> Place the most
inflexible files first, the most flexible last</p>
<p>Even within these sub-groupings of <tt class=
"filename">system/project/include</tt> I tend to group the
individual includes on a &quot;least flexible first&quot; strategy, although
I don't get hung up on this unless it causes a problem: so for
example, I would include a third party library above a locally
developed one.</p>
<p>I find that the three groupings work well, however, you may wish
to take it further to reflect the layering of your system more, or
to fit IDL files in logically (I'd suggest between system and
project files) although I don't think any purpose is served in
codifying the rule to the n'th degree.</p>
<p><span class="bold"><b>Rule 15:</b></span> Place any forward
declarations after the local includes</p>
<p>For example:</p>
<pre class="programlisting">
// local includes 
#include &quot;Employee.h&quot;
// forward declarations
class Name;
</pre>
<p>Again, following the most easily changeable rule you can change
these on the spot in this file - although this may have a ripple
effect.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e371" id="d0e371"></a>Break</h2>
</div>
<p>These rules-of-thumb are the rules I apply every time I code
header files. Next month I hope to continue with a few more rules
and say a few words about common problems with include files and
debugging techniques specific to include problems.</p>
</div>
<div class="footnotes"><br>
<hr class="c2" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e58" href="#d0e58" id=
"ftn.d0e58">1</a>]</sup> Henney's advice on avoiding .h as an
extension is new to me. He makes a good case which I look forward
to trying out.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
