A question on style regarding includes (C++)

Started by
29 comments, last by SiCrane 11 years, 1 month ago

[Removed]

Advertisement

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.

Also, prefer forward declarations instead of including class headers where possible.

[Removed]

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

[Removed]

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

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

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

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?

This topic is closed to new replies.

Advertisement