0

Thanks for reading my question. I have a class which reads a configuration and based on the configuration, populates a data structure by reading from a database, which is ultimately presented to a processing layer that makes use of the data for various processing.

Configuration:

<FieldConfigs>
    <ConfigField name="MyDBCol1"/>
    <ConfigField name="MyDBCol2"/>
    <ConfigField name="MyDBCol3"/>
</FieldConfigs>

I store my data on a std::map with string keys. As the data fields are defined in the configuration, if someone makes the mistake of not adding the configuration entries to the configuration files but tries to access them in the code, it would require a check every time data is accessed, to avoid crashes. Since this is a setup issue, it will be counter productive to run the check every time data is accessed.

To detect the setup errors, I have to run a check when the program starts whether all the fields accessed by the program are actually defined in the configuration file. Since I'm trying to avoid a developer mishap, the ideal solution should throw compiler errors if the check fails. Third party library usage have to go through an approval process by the client which can take months, and I want to avoid them if possible.

So far I have tried these avenues:

  • I tried solving this problem using reflection, but all implementations I tried, require some intervention by the developer as C++ does not provide object metadata by default. Since, we are trying to avoid a developer mistake, I doubt that adding another thing for the developer to do (which can easily be missed) would be of much use to avoid the problem I'm trying to solve.
  • Another option will be to force the developer to register all the map keys in advance via a separate register(std::string FieldName) method at the program startup and then validate the configuration file fields against it.
  • Last method I thought was to run the actual processing method with some mock data on the DB. If any undefined field (in configuration) is used, then the errors will be fired before the BAU.

What I still wonder is whether there is a better way to handle this problem than above methods to make this implementation future-proof.

1 Answer 1

1

I store my data on a std::map with string keys. As the data fields are defined in the configuration, if someone makes the mistake of not adding the configuration entries to the configuration files but tries to access them in the code, it would require a check every time data is accessed, to avoid crashes.

As std::map can also automatically insert new (default constructed) items when a key is used that isn't present yet, you could have even worse behavior than a crash. The program could appear to work but give (subtly) wrong results.


If the configuration file is already available at build time, then you could write a small script that reads the configuration file and creates a class that wraps the std::map and provides an actual function for each ConfigField in the configuration.
When the developers then code against this generated wrapper class, they will get a compiler error if they try to use a function that doesn't exist.

Most build tools allow you to automatically call such scripts as part of the build process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.