Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to put all project headers into one file HEADERS.h?

Tags:

c++

header

I talked to my instructor the other day and asked him this question. He told me that I could go for smaller projects, but I'm starting a chess program and I was wondering what Stack Overflow thinks about this issue. Should I include all headers into one file, or separate them?

like image 506
TheFuzz Avatar asked Nov 20 '09 03:11

TheFuzz


2 Answers

Normally, you want separate headers.

Including more than necessary does a few potentially bad things.

  1. This is single greatest cause of slow compile times. Unnecessary inclusion of extra headers slows down compilation, since each source file has to worry about more info than it needs. It starts as a small problem, and before you know it hundreds of developers are wasting dozens to hundreds of hours each because the problem got too far out of hand to fix. Even though you're working on small problems, it's important to understand proper separation of headers - it's easy to get right the first time but very hard to fix later if you ignore it.

  2. You lose the ability (depending on compiler/IDE) to correctly handle dependencies, and wind up building more information than you need to build.

  3. You lose maintainability. It's more difficult to maintain software when you have large, single files, than when you have lots of small, concise files.

  4. You make the code more difficult to understand, hence more prone to bugs. By having everything in one file, it's easier to get "lost" in the huge header.

like image 168
Reed Copsey Avatar answered Sep 22 '22 17:09

Reed Copsey


There are some times when this practice is useful:

  1. Pre-compiled header files with code that really doesn't change that often.

  2. APIs

like image 31
Matt Avatar answered Sep 25 '22 17:09

Matt