08.28
recently at work, i stumbled across a problem that really isn’t stressed much in school: globals. bear with some of my vagueness as i can’t really go into details about what the application does.
an application i was working on needed to store some hardcoded globals due to the requirements. most of the constants were strings (stl) that pointed to locations of files and so forth. given the environment and the budget provided, it was the easiest (not necessarily most elegant) solution to stash them as static const globals.
eventually, i realized that some of these globals contained only part of the necessary information and that the rest is discovered at run-time (has to be done that way). this is where static became a problem. see i made the grave mistake of placing these statics in a header file, which is apparently a big no-no because of this exact reason. each time they were included in a file (typically a .cc file), that file received its own copy. thus, any modifications in other files were not reflected accordingly.
so i asked some of the senior level developers in the area what they would recommend to maintain this set of mostly constant global data. apparently, only inexperienced developers make this mistake and the fact that i have discovered the errors of my ways this early in my career is a good thing (i was told that many developers still make those mistakes, which is why they are still SWE 1).
the general consensus is that the most elegant solution is to make an entirely separate object and use it to store all the global data. in this object’s constructor is where i should set the values to their defaults. this object should be made global in my main entry point file (file that contains the main) and instantiated inside of the entry point (main). everywhere that this object is needed, it should be externed.
after 6+ hours of fiddling with the codebase and trying to get it to compile and operate as necessary, i have finally achieved a finely tuned way of storing (mostly constant) global data. to make matters even better, this technique finally let me decouple the original header and those constants from each object and module that did not need it.
as a side note, with the changes made, this pushed the line count of the application to above 20K. every line was written by hand from scratch.
/me wipes brow
No Comment.
Add Your Comment