mardi 12 janvier 2016

How to use information from configuration (file) in a program

For some time now I wonder what the "correct" way is to handle information from a configuration resource (file, database, cookie, ...) in a program. I am planning to use C++, but I think the question is generic.

What I mean is that, assuming I have a mechanism to fetch configuration information, how do I store and use it in my program?

For the sake of an example, let's assume I have a config file that contains a value. Like

debug = true

(The value could also live in a DB or be fetched via a call to a web service or whatever; I do not care.)

In my code, I could now

  • set a "global" variable, e.g. DEBUG, according to the value from the config and access that variable whenever I need it
  • have an object called config that has a property debug, set that property according to the config and then "pass around" that config object to wherever I need it (How? As a method argument?)
  • fetch the value of debug every time I need it
  • ...

What is the best practice for this? Where can I find more information about this?


This is C++ specific:

Assuming I would want to set a "global" variable, how do I do that? Have a config.h file that declares the debug variable and include it everywhere? Then make sure the values are set before they are used?


Thanks for any hint and opinion!

Ps.: I found Design guidelines for configuration system? on stackoverflow. That's basically the same question, but rather Java specific. There are also other similar questions (e.g. Design pattern and configuration settings or What is a good approach while implementing the configuration settings of a program?), but somehow they are not exactly what I am looking for.

Aucun commentaire:

Enregistrer un commentaire