Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing the number of includes in a cpp file [closed]

Tags:

c++

I currently have about 50 or more includes in my cpp file. I wanted to know whats the best policy of organizing this situation. Should I continue adding more includes ? or should I take another approach ? If so what should that be ?

like image 643
Rajeshwar Avatar asked Jan 22 '26 23:01

Rajeshwar


1 Answers

1) Limit the scope of each file to one class, or to a small group of related classes with related behavior.

If your cpp file is only 400 to 1000 lines of code, it'll be pretty hard to require dozens of includes.

Also, as you move things around and make modifications, re-evaluate headers included in each file. If you originally implemented something using a vector, but then switched to set, consider removing the #include < vector >

2) In header files, use forward declarations, move those includes to the cpp file.

This doesn't address your question directly, but it is related in managing compile time. If you can get away with a forward declaration in a header file, do that.

If a.h includes b.h and b.h includes c.h, whenever c.h changes, anything including a.h has to recompile as well. Moving these includes to the .cpp file (which isn't typically #included) will limit these cascading changes.

Let's re-evaluate what happens if a.h forward-declares classes defined in b.h, and a.cpp includes b.h, and if b.h forward-declares classes defined in c.h, and b.cpp includes c.h.

When c.h changes, b.cpp needs to recompile, but a.cpp is fine.

3) Re-organize as you extend functionality.

Writing code can happen like this:

  1. Plan to implement a feature.
  2. Write code to support that plan. (If doing unit testing, consider writing the test first)
  3. Debug and ensure the code meets your requirements.

But this is missing a few steps that make a huge difference.

  1. Refactor your code, and remove extraneous includes. (Here's where unit testing helps.)
  2. Debug again.

These last two steps can involve splitting up large functions into several smaller ones, breaking classes up, and (relevant to this question) creating new files for functionality which has out-grown its current compilation unit.

If you move on the moment your code seems to work and do not take a moment to tidy up it is the equivalent of a carpenter hammering legs onto a table, but never taking a moment to sand and polish the result. Sometimes it's okay to have a slab of plywood with some legs, but if you want a nice dining room table, you'll find this approach is not up to the task.


There is no simple way of reducing includes for cpp files, ultimately those includes need to exist somewhere (provided the original code relying on those includes is still present, if it is not, then simply delete the extraneous include), and the .cpp file is way better than the .h file. But with careful management and organization you can maintain smaller, more modular source code. This is ultimately how you keep things from becoming unworkable.

like image 153
M2tM Avatar answered Jan 24 '26 15:01

M2tM