♦ 🐆 2 min, 🐌 4 min
unsigned int num_of_users = 1401;
Before you know it, the variable num_of_users
is used in ten places all over the code. Then you update num_of_users
to 1402
. But forget to update the variable at some of the places in the code. The code works but gives inconsistent results. After hours of searching for the needle in the haystack, you figure out what you forgot to update. OK, problem solved. Two days later, you're pulling out your hair because another variable is causing trouble.class Parameters {
pubic:
unsigned int num_of_users;
// rest of parameters
Parameters() {
this->num_of_users = 1501;
}
};
Then with initialization:Parameters var;
we get access to all parameters via:var.num_of_users;
auto var = std::make_shared();
We can then pass the var
smart pointer into any part of the code/module that needs access to those parameters. For example, we need to pass the parameter to two processing modules:ProcessingModuleA processing_module_a(var);
ProcessingModuleB processing_module_b(var);
Where ProcessingModuleA
is defined as:class ProcessingModuleA{
public:
// rest of the module code
ProcessingModuleA(
const std::shared_ptr &var){
var.num_of_users; // accesible
// rest of the module code
}
};
C++
code once and then control the processing output via a JSON file. Advantages:parameters.JSON
file as the input file the implementation becames a bit more technical. Example class implementation below:#include
#include
#include "json.hpp"
class Parameters {
pubic:
unsigned int num_of_users;
// rest of parameters
Parameters(
const std::string ¶meters_location) {
json parameters = read_json_file(parameters_location, "parameters.JSON");
try {
this->num_of_users = parameters.at("num_of_users").get();
}
catch(std::range_error &e) {
std::cout << e.what() << std::endl;
throw;
}
catch (...) {
std::cout << "Unknown exception occurred" << std::endl;
throw;
}
}
};
You should use try
and catch
block to ensure that you catch any errors that occur while reading the JSON
file. On what read_json_file
function does check the article JSON in C++: Read & Write JSON
file gives us a tremendous amount of speed up in the data analysis. As a result, we can get insights faster. Insights are what the whole big data and analytics are about anyway.
Get notified & read regularly 👇