• Create Account

# A question on style regarding includes (C++)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

30 replies to this topic

### #1Shaquil  Members   -  Reputation: 734

Like
0Likes
Like

Posted 18 February 2013 - 04:07 PM

I'm working on a project, and there are many different includes amongst multiple header files. For example, if I have a header called "GameHeader.h" and three others headers "Attack.h", "Animation.h", and "Engine.h", you might see Engine.h included in all of the other three, and Animation.h and Attack.h both included in GameHeader.h

In other words, GameHeader.h looks like this:

#include "Engine.h"
#include "Attack.h"
#include "Animation.h"


I have of course protected myself with #ifndef. That's not the question. My question is, if Animation.h already includes "Attack.h" and "Engine.h", should I just cut those three lines to one?

#include "Animation.h"


I feel like that'll send potential readers (i.e. myself, a year from now) chasing through header files one by one, when I could just be honest upfront and show everything I'm including, even if some of it's redundant. But I'm a beginner, so what I think is questionable. What do you guys do in this situation?

### #2L. Spiro  Crossbones+   -  Reputation: 7291

Like
5Likes
Like

Posted 18 February 2013 - 04:12 PM

A file has no idea what files it includes include.  It should include exactly what it needs for all of its parts to work regardless of any redundant includes that may possibly exist.

Additionally, they should be included from the translation unit (.C/.CPP file) when possible.

L. Spiro

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013

I went to my local Subway once to find some guy yelling at the staff.  When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #3ApochPiQ  Moderators   -  Reputation: 10426

Like
9Likes
Like

Posted 18 February 2013 - 04:13 PM

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.
Maker of Machinery

### #4King Mir  Members   -  Reputation: 1304

Like
2Likes
Like

Posted 18 February 2013 - 05:57 PM

Include what is used directly by the file. So if you use "Engine.h" and "Attack.h" separately from the implied use though using animations, include them.

### #5Shaquil  Members   -  Reputation: 734

Like
0Likes
Like

Posted 18 February 2013 - 06:09 PM

A file has no idea what files it includes include.  It should include exactly what it needs for all of its parts to work regardless of any redundant includes that may possibly exist.

Additionally, they should be included from the translation unit (.C/.CPP file) when possible.

L. Spiro

Do you mean that I should write:

#include "Engine.cpp"


rather than

#include "Engine.h"


?

### #6L. Spiro  Crossbones+   -  Reputation: 7291

Like
4Likes
Like

Posted 18 February 2013 - 06:14 PM

No, I mean inside of Engine.cpp you should include headers that do not need to be included from Engine.h.

If you are using references or pointers to class B inside your class declaration for class A, you should forward-declare class B inside A.h and inside A.cpp you should #include "B.h".

L. Spiro

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013

I went to my local Subway once to find some guy yelling at the staff.  When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #7Shaquil  Members   -  Reputation: 734

Like
0Likes
Like

Posted 18 February 2013 - 06:23 PM

No, I mean inside of Engine.cpp you should include headers that do not need to be included from Engine.h.

If you are using references or pointers to class B inside your class declaration for class A, you should forward-declare class B inside A.h and inside A.cpp you should #include "B.h".

L. Spiro

Oh, I understand! That's actually a really smart way to do it. So if I need <math.h> for some function I'll be defining in Engine.cpp, I shouldn't include <math.h> in Engine.h unless I absolutely have to.

### #8L. Spiro  Crossbones+   -  Reputation: 7291

Like
3Likes
Like

Posted 18 February 2013 - 06:24 PM

Additionally, if you are using C++, you should include the C++ headers rather than the C headers.

#include <math.h> #include <cmath>

L. Spiro

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013

I went to my local Subway once to find some guy yelling at the staff.  When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #9Polarist  Members   -  Reputation: 160

Like
0Likes
Like

Posted 18 February 2013 - 06:38 PM

So of course, it doesn't *really* matter if you include everything as long as it compiles.

The typical rule of thumb, however, as mentioned above, is to include as little as possible in each place (both in the .h and in the .cpp, meaning that most includes per translation unit should end up in the .cpp file).  This is primarily for 2 reasons, managing compile time and managing dependencies.

Compile time is obvious, because every time you modify something and something else includes it, the latter will need to be compiled again.  And clearly, you'd rather spend more time coding and testing than waiting for things to compile.

Managing dependencies is the other, you can use your includes to document how many dependencies each file has.  As a program gets to be complex, you generally want pieces of your program to be as independent as possible from the other pieces.  Thus, you can evaluate how many dependencies something has by looking at how many includes there are at the top.  (This heuristic works only if you followed the rule above of having as few includes as possible.)

But of course, this rule shouldn't always be followed 100%.  Especially if you're working in a small team or individually.  There's a competing idea of optimizing for coding time ("optimizing for life"), which means that you should do these things only to the point at which you are actually gaining time in the long run.

If you find that having some often-used things in a big "globals.h" include saves you time in the end, and you know you can manage your project's complexity well, then you should consider putting stuff in the global include file to save you typing time in the end.  For instance, if you wrote a run-time debugger, profiler, or even a global game state that you need to query almost everywhere, then just put it in the globals.h to save you typing time in the end.  If you know where your dependencies are, you can always fix up your includes later if you wish.  Especially towards the beginning of a project, when you are still prototyping a lot of different systems.  I think it makes sense to use larger global includes.

Other people may have other wisdom to share in this regard though, as when and where to use global includes is pretty subjective.

### #10Servant of the Lord  Crossbones+   -  Reputation: 12557

Like
0Likes
Like

Posted 19 February 2013 - 01:38 AM

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #11EWClay  Members   -  Reputation: 655

Like
1Likes
Like

Posted 19 February 2013 - 02:50 AM

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

Presumably he means that SpecificWidget.h will include BaseWidget.h. It would be quite a time waster if in MainDialog.h you had to figure out what SpecificWidget needed.

There is, by the way, a trick to ensure that headers include or forward declare everything they need. In SomeClass.cpp, the first include (other than precompiled headers) should be SomeClass.h. Then if that compiles, SomeClass.h has no additional dependencies beyond what it includes or declares itself..

### #12SiCrane  Moderators   -  Reputation: 8490

Like
3Likes
Like

Posted 19 February 2013 - 06:49 AM

Additionally, if you are using C++, you should include the C++ headers rather than the C headers.
#include <math.h> #include <cmath>

Keep in mind if you do this you should prefix the standard library math functions with std::. According to the standard, if you include <cmath> the declaration go in the std namespace, but may also go into the root namespace. If you include <math.h> the declarations go into the root namespace, but may also go into the std namespace. If you don't want to use the std:: prefix, including <math.h> is preferable, because if you only include <cmath> the non prefixed version is non portable.

### #13Alundra  Members   -  Reputation: 421

Like
0Likes
Like

Posted 19 February 2013 - 09:15 AM

A global header is an all-in-one use to have access to all the engine but the compilation time is higher.

Edited by Alundra, 19 February 2013 - 09:15 AM.

### #14L. Spiro  Crossbones+   -  Reputation: 7291

Like
2Likes
Like

Posted 19 February 2013 - 09:26 AM

It is bad for exactly both of the reasons you just mentioned.

All-in-one is by definition a violation of the single-responsibility principle, which applies not only to objects but to headers, which do have responsibilities of their own.

All-in-one is also by definition a violation of modular design.

Yes it is bad to use a global header, because it is all-in-one and increases compile times.

L. Spiro

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013

I went to my local Subway once to find some guy yelling at the staff.  When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #15Servant of the Lord  Crossbones+   -  Reputation: 12557

Like
1Likes
Like

Posted 19 February 2013 - 04:06 PM

My rule of thumb is that every file (including headers) should include precisely what it relies on to compile, to a limit of one level of indirection.

So MainDialog.h would include SpecificWidget.h, but won't bother including BaseWidget.h.

How's that possible? If SpecificWidget inherits BaseWidget, BaseWidget can't be pre-declared - inheritance requires the full declaration, doesn't it?

Presumably he means that SpecificWidget.h will include BaseWidget.h. It would be quite a time waster if in MainDialog.h you had to figure out what SpecificWidget needed.

If he meant, "MainDialog.h includes BaseWidget.h, but not SpecificWidget.h, and MainDialog.cpp would include SpecificWidget.h, which in-turn includes BaseWidget.h", that makes sense. But SpecificWidget.h must include BaseWidget.h if it inherits it, afaik.

My biggest problem is that, by favoring composition, I then have to include alot of minor classes like Rect, which includes Point and Size. I also have to include things like string and vector and map frequently. Those kind of things are included so much, it's ridiculous, and being member-variables, they have to be #included by the header file.

Well, I guess that's one of the benefits of the pImple idiom? Keeping your private composition out of the header file to improve compile times?

Precompiled headers unfortunately have been too buggy when I've used them with the tools I use, that I just ignore pre-compiled headers for now.

I ran HeaderHero the other day on my code - it's annoying to see some of the headers being included 35 thousand times or more.

If only C++'s module system was complete, it'd automagically solve alot of these issues.

Edited by Servant of the Lord, 19 February 2013 - 04:07 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #16Brother Bob  Moderators   -  Reputation: 6420

Like
2Likes
Like

Posted 19 February 2013 - 04:35 PM

If he meant, "MainDialog.h includes BaseWidget.h, but not SpecificWidget.h, and MainDialog.cpp would include SpecificWidget.h, which in-turn includes BaseWidget.h", that makes sense. But SpecificWidget.h must include BaseWidget.h if it inherits it, afaik.

That's correct. MainDialog only depends on SpecificWidget, so there is no reason why MainDialog should include anything else. If SpecificWidget itself has to include something to work is not MainDialog's problem. For what it's worth, there may not even be a BaseWidget header but that SpecificWidget defines it itself. Either way, MainDialog has no reason to care how exactly SpecificWidget defines itself, only that it is defined.

That is the "one level of indirection"; only include the direct dependencies, and let the dependencies sort out their own dependencies themselves.

### #17ApochPiQ  Moderators   -  Reputation: 10426

Like
0Likes
Like

Posted 19 February 2013 - 04:35 PM

I meant to imply that SpecificWidget.h would include the things it needs to compile; assuming inheritance is used, that means SpecificWidget.h is responsible for including BaseWidget.h.
Maker of Machinery

### #18EWClay  Members   -  Reputation: 655

Like
0Likes
Like

Posted 19 February 2013 - 04:35 PM

Technically it's possible for SpecificWidget.h not to include BaseWidget.h, as long as everything that includes SpecificWidget.h already includes BaseWidget.h. It's not a good idea, though.

String, vector and map I put in the precompiled header. They work fine for me. Small, commonly used classes like Rect can be put into small headers that don't include anything else. The real problem is when you bring in a whole system for something that's only used in the implementation.

It can be hard to keep on top of it. Every large project I've worked on has had this problem. Pimpl and dependency injection help, and everyone has to be really disciplined about forward declarations and not adding unnecessary headers.

### #19Servant of the Lord  Crossbones+   -  Reputation: 12557

Like
0Likes
Like

Posted 19 February 2013 - 04:56 PM

I meant to imply that SpecificWidget.h would include the things it needs to compile; assuming inheritance is used, that means SpecificWidget.h is responsible for including BaseWidget.h.

Ah, that makes sense - I do that too. I thought you meant that your header depths are only ever 1+1 deep, which is nigh impossible.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #20Shaquil  Members   -  Reputation: 734

Like
0Likes
Like

Posted 20 February 2013 - 09:47 AM

Another small question I have is on where to put the "using namespace". I know it's probably better not to always put that using statement up at the top like that, but sometimes it's necessary. Here's an example from a file I've written:

#ifndef MOUSE_H
#define MOUSE_H
#include "GenericEntity.h"
using namespace gen;
#include <vector>
using namespace std;


GenericEntity.h has a namespace gen in it that I want to access. And vector uses std obviously. So I put the using namespace declarations under their respective headers. Is that a dumb way to do it?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS