I am a solo developer on a large C++ library that I use for research (I’m a PhD student). Let’s say the library has a bunch of classes that implement cool algorithms: Algorithm1, Algorithm2, etc. I then write a bunch of C-style functions that are stand-alone “scripts” that use the library to either test the recently added functionality or to run simulations that produce plots that I then include in wonderfully-brilliant (I’m in denial) journal publications. The design of the library follows good software engineering principles (to the best of my knowledge and ability), but the “scripts” that link the library from main.cpp do not follow any principle except: “get the job done”.
I now have over 300 such “scripts” in a single file (20,000+ lines of code). I have no problem with it, I remain very productive, and that’s really the ultimate goal. But I wonder if this approach has major weaknesses that I just have learned to live with.
// File: main.cpp
#include <cool_library/algorithm1.h>
#include <cool_library/algorithm2.h>
...
#include <cool_library/algorithmn.h>
void script1() {
// do stuff that uses some of the cool library's algorithms and data structures
// but none of the other scriptX() functions
}
void script2() {
// do stuff that uses some of the included algorithms and data structures
}
...
// Main function where I comment in the *one* script I want to run.
int main() {
// script1();
// script2();
// script3();
...
script271();
return 0;
}
Edit 1: There are several goals that I have in this process:
- Minimize the time it takes to start a new script function.
- Make all old script functions available at my finger tips for search. So I can then copy and paste bits of those scripts into a new one. Remember this is NOT supposed to be good design for use by others.
- I don’t care about the compilation time of the script file because it compiles in under a second as it is now with the 20,000 lines of code.
I use Emacs as my “IDE” by the way, in Linux, using the Autoconf/Automake/Libtool process for building the library and the scripts.
Edit 2: Based on the suggestions, I’m starting to wonder if part of the way to increase productivity in this scenario is not to restructure the code, but to customize/extend the functionality of the IDE (Emacs in my case).
If I were you, I would split that huge file into 300 smaller ones: each would have just one
scriptNN()andmain()calling just it.Now, when you have it compiled, you will have 300 small
scriptNNexecutables (you may need to create appropriateMakefilefor this though).What’s nice about this – now you can use these script executables as building blocks to be put or called by other scripts, like bash, python, perl, etc.
EDIT Explanation how this design allows to address your goals.
Time to start new script function – simply copy one of existing files and tweak it a little.
Make all old script functions available at my finger tips for search – emacs can do multi-file search across all other script files you have.
I don’t care about the compilation time of the script file – it does not matter then. But you will have all of them available to you at once, without editing one big
main()and recompiling.