CPP HEADER FILE BEST PRACTICES - rFronteddu/general_wiki GitHub Wiki

  • Always include header guards.
  • Do not define variables and functions in header files.
  • Give a header file the same name as the source file it’s associated with (e.g. grades.h is paired with grades.cpp).
  • Each header file should have a specific job, and be as independent as possible. For example, you might put all your declarations related to functionality A in A.h and all your declarations related to functionality B in B.h. That way if you only care about A later, you can just include A.h and not get any of the stuff related to B.
  • Be mindful of which headers you need to explicitly include for the functionality that you are using in your code files, to avoid inadvertent transitive includes.
  • A header file should #include any other headers containing functionality it needs. Such a header should compile successfully when #included into a .cpp file by itself.
  • Only #include what you need (don’t include everything just because you can).
  • Do not #include .cpp files.
  • Prefer putting documentation on what something does or how to use it in the header. It’s more likely to be seen there. Documentation describing how something works should remain in the source files.