I am interested in getting some opinions on the use of include files to sub-divide a large module into functionally related groups. What I'm interested in is opening a discussion on best practices and minimal requirements. Here's the scenario:
I am working with a library with 200+ functions. They naturally divide into 2 sets: and advanced set of calls and a "simplified set" which fills in some of the more arcane, rarely used arguments with typical defaults and then calls the advanced functions. So, instead of having one file with 2000+ lines of code, you have a master file with some functions in it that includes the other functions. Something like this:
master.cpp: Contains non-categorized, general function, misc stuff
master.h : Contains the usual stuff needed to make the program
advanced.cpp: Contains functional code for the advanced functions
advanced.h: Contains function prototypes for advanced.cpp
simplified.cpp: Contains functions that call things in Advanced.cpp
simplified.h : Contains prototypes for functions in simplified.cpp
The trick is that master.h includes advanced.h and simplified.h while
master.cpp includes advanced.cpp and simplified.cpp.
This has the following advantages:
1) With proper structuring of the includes, it is really easy to add/remove entire feature sets.
2) Studying the source and looking for specific code blocks gets easier because there's less to look through
3) It's easier to structure the code in an organized fashion
This idea is somewhere between the old Fortran "File per Function" paradigm and the more recent tendency to pack everything into a few mega-files. I already know that the strategy works (I use it now all the time). What I hope to get from this posting would consiste of ideas about where the dangers might be and how to work around them.
For example. to prevent multiple inclusions from generating multiple definition errors the typical include file usually has something like this:
#ifndef STRUCTS_H #define STRUCTS_H ...BODY OF FILE #endif // STRUCTS_H
In this case, you would never want one of these files included more than once. I suspect that the above code might actually make this problem worse. In some ways I think that leavin the ifdefs absent completely would be better since then a second instance of the include would certainly bomb the compiler - which might be better than some arcane error produced by a multiple inclusion.
Edited 2 Years Ago by Dani: Formatting fixed