- Should I have one file for each class? Should I have one folder for each inheritance branch?
Thanks to code indexers, I often forget how and where the code is actually located on disk.
The file for the class does not make sense to me. Especially in C ++, I tend to have many small utility classes.
Typically: a component is a directory. All components live under one root. (This is more likely an unfortunate consequence of the fact that I have to use make as a build chain. With more advanced build systems like SCons that support recursion, this doesn't really matter much at first.)
I adhere to the general principles of reuse, sufficiency and cohesion (according to Booch ) for grouping classes. It often happens that I put code in a shared / static library to facilitate integration into the assembly chain, so I use principles very similar to grouping classes into components / directories that are used to group methods into classes.
- Should I have an include folder containing my header files, or should my header files be in the same folder as the .cpp / .c files?
I personally prefer the headers in the same directory with the source files.
A notable exception is public headers, which define the public interface of a component. The ones I put in include / compname / sudbirectory: then it's easy to see that the change I'm making / about to register will have an effect on other components.
Obviously, other components are allowed to include only the headers from the $ ROOT / compname / include / compname / subdirectory. (With compname / dir in include / make, include directives in other files to look like #include "compname/headername.h" , which helps readability and also prevents header name conflicts.)
- I plan to regularly add additional classes (adding more levels to the inheritance tree). At the lowest level in the tree, implementations are likely to be relatively unrelated, but still override the same virtual functions. Should such unrelated implementations be in the same folder?
Inheritance rarely refers to the physical location of files.
Base classes rarely change because they have little effect on business logic. Top-level classes are where the meat of logic is, and many of them will be processed for a long time. Thus, core classes reused by many components deserve to have their own component. Just move them out of sight.
This is another important point. How many people are going to work on the project? In a single-person project, you can do almost anything you want, as you want. In a team of 3 people, the component / module will serve as a natural container so that one person can independently work on it. Thus, the design of the form may not depend on what the class hierarchy looks like - rather, the places of the code that will be processed on the majority should be sufficiently isolated from each other. And the class hierarchy, if necessary, should take this into account: you should avoid cases when one person works in a base class - another on his descendant. This is the case when, instead of inheritance, aggregation can be considered.