Breaking Programs Apart in C++
The programmer can break a single program into separate source files generally known as modules. These modules are compiled into machine code by the C++ compiler separately and then combined during the build process to generate a single program.
These modules are also known by compiler geeks as C++ translation units. The process of combining separately compiled modules into a single program is called linking.
Breaking programs into smaller, more manageable pieces has several advantages. First, breaking a program into smaller modules reduces the compile time. Very large programs can take quite a while.
In addition, recompiling all of the source code in the project just because one or two lines change is extremely wasteful. It’s much better to recompile just the module containing the change and then relink it into all of the unchanged modules to create a new executable with the change. (Compiling usually takes longer than linking.)
Second, it’s easier to comprehend — therefore, easier to write, test and debug — a program that consists of a number of well-thought-out but quasi-independent modules, each of which represents a logical grouping of functions. A large, single source module full of all the functions that a program might use quickly becomes hard to keep straight.
Third is the much-vaunted specter of reuse. A module full of reusable functions that can be linked into future programs is easier to document and maintain. A change in the module to fix some bug is quickly incorporated into other executables that use that module.
Finally, there’s the issue of working together as a team. Two programmers can’t work on the same module (at least not very well). An easier approach is to assign one set of functions contained in one module to one programmer while assigning a different set of functions in a different module to a second programmer. The modules can be linked together when ready for testing.