Code size overhead by including unnecessarily extra header files

clean codeheadershigh performancememorymemory usage

I have a program which includes lots of header files but it do not uses all the header files. I have removed some of them although it is working fine. I did not notice any changes in the performance.
Will this affect code size or something else.
Can I include header files as much as I want without affecting the code size and performance.
I mean if I include all headers in a separate header file then I call only this header file in my all programs. Will it work normally?

Best Answer

Generally speaking, including extra header files shouldn't increase the size or impact the performance of your compiled code, but it's still a very bad practice.

When you're working on a project that has hundreds or thousands of source files, you want to rebuild as few files as possible when making a change to a header file. If every source file includes every header file, then you'll need to rebuild everything any time you touch any header file.

On a related note, whenever possible use a forward declare instead of including a header file in another header file, for example, do this:

class bar;
class foo
{
  private: bar* that;
}

instead of:

#include <bar.h>
class foo
{
  private: bar* that;
}
Related Topic