• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Liuqahs15

A question on style regarding includes (C++)

30 posts in this topic

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.

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites


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..
1

Share this post


Link to post
Share on other sites

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

Is it really bad to use a global header ?

Edited by Alundra
0

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites

 

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. mellow.png

Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites

 
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.

2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

You shouldn't have any using-statements at file-scope in a header file at all. Either limit the scope so that the using-statement does not extend into the full scope of the translation unit, or fully qualify any name in the header file.

 

I think I understand why you say that, but could you explain it for me anyway?

0

Share this post


Link to post
Share on other sites

Instead of having using-statements in the header file, use the fully qualified name. That is, do this:

#include <vector>
void myFunc(std::vector<myClass> const &);

and not this:

#include <vector>
using namespace std;
void myFunc(vector<myClass> const &);
Edited by Brother Bob
1

Share this post


Link to post
Share on other sites
You should never put using namespace in a header. It removes the entire point of the namespace and can cause problems for any code that includes it.

I would not have using namespace at all, it makes the code clearer to be specific about where things come from. If you have to do it, put it in the smallest scope possible.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0