Friday, 20 July 2018

Modular style

So what I have discovered lately is a sort of modular programming style where classes and functions of same theme are placed in one source file. This doesn't sound like very interesting news, but at least in my experience it may solve some problems resulting from C++ "class per file" rule we have been told to follow.

The problem with "class per file" is the physical size and complexity of the project: the more files it has the more difficult it becomes to maintain. C++ doesn't help, because you have to remember to include headers and also keep track of headers that are no longer needed. With large, over 300+ files project it starts to become a hassle.

Modular style has also other benefits like keeping better track of similar types of classes/routines and possibly making them more generic with inheritance etc. If your classes are in a single file you may actually forget(!) something similar exists. It's easier to find stuff, too, when similar type of code is in one file.

In C++ the downside is naming, sometimes there are issues when some namespace has been opened. But it's quite simple to remedy it by not making your names too generic even it is possible with namespaces, or use nested namespaces when possible. With modules the amount of namespaces seem to drop as well, with logical solution to use namespace per module, possibly even using the same name as the filename to keep things tidy.

For me at least modular programming used to be split to classes in a way, because classes are kind of modules by themselves. But using larger thematic modules may actually prove to be very important in this project and in the way I'm able to maintain and develop it.