Header includes can be a shambles. Matthew Wilson encourages us to bring some order to the chaos.
Order includes in groups of descending specificity and lexicographically within groups
Consider the following example of
s in source file
, containing the implementation of a class
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.
#include "stdafx.h" #include <vector> #include <string> #include <acmecmn/string_util2.h> #include <acmecmn/string_util1.h> #include <Blade/Sharpener.hpp> #include <Blade/Protector.hpp> #include "Cutter.hpp" #include <stdlib.h> #include <map>
This is quite wrong. Here’s the right way to do it:
#include "stdafx.h" #include "Cutter.hpp" #include <Blade/Protector.hpp> #include <Blade/Sharpener.hpp> #include <acmecmn/string_util1.h> #include <acmecmn/string_util2.h> #include <map> #include <string> #include <vector> #include <stdlib.h>
This has been ordered according to descending order of specificity of groups, and then lexicographically within groups. The drivers are, respectively, modularity and transparency .
The reason for descending order of specificity of groups is to expose hidden dependencies –
– in any of the header files. Unlike languages such as Java and C#, the order of ‘imports’, in the form of
s, has significance in C and C++. For example, if
makes use of
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
requires a definition in
then this would also be exposed.
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 not advise – you’ll have to wear this slight risk. The grouping will take care of the vast majority.)
The reason for a blank line between groups is obvious: to delineate one group from another to further aid transparency.
Usually, the most specific group – the
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
. Note that it makes no difference whether the declaring header is in the same directory, i.e.
, or in another directory, e.g.
: its pre-eminence is unchanged.
Sometimes, for implementation reasons, we have to have a Level-0 group – in this case this is the precompiled header include file stdafx.h . (In a soontobecooked Bite I will discuss why and how you should get rid of the presence of these things from your source.)
Similarly, for very rare implementation reasons, we have to have a Level-N 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.
Hence, the rule is to order includes:
Order all files in groups of descending order of specificity, with each group separated by a blank line, including:
- Explicit Level-0 includes (if required);
include(s): the declaring header(s) file (for implementation files only);
- Other include groups for the given application component;
- Other include groups for the organisation;
- Other include groups for 3rdpartysoftware;
- Standard C++ headers;
- Standard C headers;
- Explicit Level-N includes (if required).
- Lexicographically order all includes within groups;
C++ Coding Standards , Herb Sutter & Andrei Alexandrescu, AddisonWesley, 2004
Large Scale C++ Software Design , John Lakos, AddisonWesley, 1996