Deciding between datastore and global variables

116 views Asked by At

Description

For a hybrid C/C++ (C++ wrapped in extern "C") application I'm writing I am trying to decide if it is better for me to include some static definitions my program needs to run as global variables or in an external datastore that needs to be read in every time it is run (i.e. a .csv file or sql database).

My static definitions, when laid out in a csv file, take appx 15 columns each with a maximum of 40 definitions (less than that to begin, but up to 40 due to feature scaling).

Problem

I'm torn because it feels wrong to me to include so much data as global variables that get loaded with the program at compile time. However, the overhead of reading from a datastore every time I run the program after compiling seems unnecessary.

Question

What are the best practices here? My code needs to be portable enough for someone else to understand and I do not want to obfuscate it.

3

There are 3 answers

1
too honest for this site On BEST ANSWER

It might be appropriate to generate a seperate C file from the CSV using a high level language, e.g. Python. Then either #include the generated file (only if used in a single module using static), or as a seperate compilation unit.

That way you can easily change the values in the CSV/spreadsheet program of your choice, while still having all data available. The code generating program can be called by the build system, so no manual fiddling.

0
jpyams On

For speed reasons and code clarity staticly define these variables. Lay out your definitions nicely and comment generously to help future viewers of your code. In a file, you can't comment to inform future editors what everything is. It's just slower.

0
Emacs User On

The very best practice is to implement both options with flexibility to switch between implementations based on memory, computation speeds, and other load conditions.

If the application will be run server-side with generous allocation for memory/cpu, then design to those conditions. Why "feel wrong" as you put it?

Your end goal is not clearly defined. So obfuscation is not an issue yet. Obfuscation comes when you willfully redirect your coding to hide your tracks. But making a complete solution, if that is what is needed, is not obfuscation.