    <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  :: QM Bites â€“ Order Your Includes (Twice Over)</title>
        <link>https://members.accu.org/index.php/articles/2249</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 #133 - June 2016 </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/c362/">o133</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+362/">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;QM Bites â€“ Order Your Includes (Twice Over)</h1>
<p><strong>Author:</strong>&nbsp;Martin Moene</p>
<p>
<strong>Date:</strong> 06 June 2016 13:23:16 +01:00 or Mon, 06 June 2016 13:23:16 +01:00</p>
<p><strong>Summary:</strong>&nbsp;Header includes can be a shambles. Matthew Wilson encourages us to bring some order to the chaos.</p>
<p><strong>Body:</strong>&nbsp;<h2>TL;DR</h2>

<p>Order includes in groups of descending specificity and lexicographically within groups</p>

<h2>Bite</h2>

<p>Consider the following example of <code>#include</code>s in source file <span class="filename">Cutter.cpp</span>, containing the implementation of a class <code>Cutter</code> for a fictional organisation AcmeSoftware with a product Blade. In this case, the classâ€™s implementation and header files are located in the same source directory; this need not always be so, but the discussion to follow still applies.</p>

<pre class="programlisting">
  #include &quot;stdafx.h&quot;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;acmecmn/string_util2.h&gt;
  #include &lt;acmecmn/string_util1.h&gt;
  #include &lt;Blade/Sharpener.hpp&gt;
  #include &lt;Blade/Protector.hpp&gt;
  #include &quot;Cutter.hpp&quot;
  #include &lt;stdlib.h&gt;
  #include &lt;map&gt;</pre>
  
<p>This is quite wrong. Hereâ€™s the right way to do it:</p>

<pre class="programlisting">
  #include &quot;stdafx.h&quot;
  
  #include &quot;Cutter.hpp&quot;
  
  #include &lt;Blade/Protector.hpp&gt;
  #include &lt;Blade/Sharpener.hpp&gt;
  
  #include &lt;acmecmn/string_util1.h&gt;
  #include &lt;acmecmn/string_util2.h&gt;
  
  #include &lt;map&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  #include &lt;stdlib.h&gt;</pre>
  
<p>This has been ordered according to descending order of specificity of groups, and then lexicographically within groups. The drivers are, respectively, <em>modularity</em> and <em>transparency</em>.</p>

<p>The reason for descending order of specificity of groups is to expose hidden dependencies â€“ <em>coupling!</em> â€“ in any of the header files. Unlike languages such as Java and C#, the order of â€˜importsâ€™, in the form of <code>#include</code>s, has significance in C and C++. For example, if <span class="filename">Cutter.hpp</span> makes use of <code>std::vector</code> but does not itself include that file then compilation units that include it are at risk of compile error; the original order masks that. The same rationale applies to the files in other groups: if <span class="filename">Blade/Sharpener.hpp</span> requires a definition in <span class="filename">acmecmn/string_util1.h</span> then this would also be exposed.</p>

<p>The reason for lexicographical ordering with groups is to make it easier to comprehend each groupâ€™s contents. In the real world there can be many tens of included files, and if not in some readily comprehensible order then duplicates can more easily occur, which is then obviously a problem when trying to remove unnecessary includes. (Admittedly, by having a strict lexicographical ordering within groups there is a slightly increased possibility of hiding interheader coupling between files in a group, but unless you want to go to some extreme such as reverse lexicographical ordering for headers and forward for implementation files â€“ which I do <em>not</em> advise â€“ youâ€™ll have to wear this slight risk. The grouping will take care of the vast majority.)</p>

<p>The reason for a blank line between groups is obvious: to delineate one group from another to further aid transparency.</p>

<p>Usually, the most specific group â€“ the <em>Level-1</em> group in an implementation file â€“ would be its declaring header(s), containing declarations of its API functions and/or defining its class: in this case <span class="filename">Cutter.hpp</span>. Note that it makes no difference whether the declaring header is in the same directory, i.e. <code>#include &quot;Cutter.hpp&quot;</code>, or in another directory, e.g. <code>#include &lt;AcmeSoft/Blade/Cutter.hpp&gt;</code>: its pre-eminence is unchanged.</p>

<p>Sometimes, for implementation reasons, we have to have a <em>Level-0</em> group â€“ in this case this is the <em>precompiled header include file</em> <span class="filename">stdafx.h</span>. (In a soontobecooked Bite I will discuss why and how you should get rid of the presence of these things from your source.)</p>

<p>Similarly, for very rare implementation reasons, we have to have a <em>Level-N</em> group. I have not shown such in this case, but if youâ€™ve chomped on enough C++ in your time youâ€™ll have experienced such things, perhaps to conduct some shameful but necessary preprocessor surgery after all includes but before any implementation code is translated.</p>

<p>Hence, the rule is to order includes:</p>

<ol>
	<li>Order all files in groups of descending order of specificity, with each group separated by a blank line, including:
		<ol style="list-style:lower-alpha">
			<li>Explicit Level-0 includes (if required);</li>
			<li>Level-1 <code>include</code>(s): the declaring header(s) file (for implementation files only);</li>
			<li>Other include groups for the given application component;</li>
			<li>Other include groups for the organisation;</li>
			<li>Other include groups for 3rdpartysoftware;</li>
			<li>Standard C++ headers;</li>
			<li>Standard C headers;</li>
			<li>Explicit <em>Level-N</em> includes (if required).</li>
		</ol>
	</li>
	<li>Lexicographically order all includes within groups;</li>
</ol>

<h2>Further Reading</h2>

<p class="bibliomixed"><em>C++ Coding Standards</em>, Herb Sutter &amp; Andrei Alexandrescu, AddisonWesley, 2004</p>

<p class="bibliomixed"><em>Large Scale C++ Software Design</em>, John Lakos, AddisonWesley, 1996</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
