How to handle large number of configuration parameters across a program conceptually? -


imagine software system have 100 parameters, each of sensible change (in case reasearch system machine learning). obvious solution store parameters in config file, system easy handle , see through.

whis approach better:

  • a) load config file @ entry point of program , pass down big collection of config variables through each method in code
  • b) load config file @ entry point of program , pass down relevant collection of config variables through each method in code
  • c) load config variables directly needed
  • d) load config , make global

i'm open suggestions or examples of particular implementation. @ moment i'm experimenting nested config variables, each nested object storing config of different modules in code.

i recommend following steps.

first, use configuration file syntax supports concept of multiple sections/scopes, , preferably nestable scopes. example, xml element nestable scope in sense talking about, while ".ini" file provides non-nestable sections. java properties file not provide direct support scopes, can emulate such support using syntax x.y.z denote variable z, in scope y, in turn nested in scope x. config4* (which developed) provides direct support nested scopes.

second, write constructor of configurable foo class shown in following pseudocode:

foo(configuration cfg, string scope) {     _x = cfg.lookup(scope + ".x");     _y = cfg.lookup(scope + ".y");     _z = cfg.lookup(scope + ".z");     _bar = new bar(cfg, scope + ".bar"); } 

in above pseudo code, use _ prefix denote instance variable, , assume configuration class has lookup() operation takes scoped name, example, cfg.lookup("foo.bar.abc") return value of abc variable in foo.bar scope.

third, main() function of application can written shown in following pseudocode:

main(...) {     string configfilename = ...; // obtain command-line argument     string scope          = ...; // obtain command-line argument     configuration cfg     = parseconfigurationfile(configfilename);     foo foo               = new foo(cfg, scope + ".foo");     ... // create other configured objects     dorealwork(foo, ...); } 

finally, command-line arguments application should specify: (1) name of configuration file, , (2) top-level scope (within configuration file) holds configuration variables running application. example, let's assume example.cfg structured follows:

instance1 {     foo {         x = "a value";         y = "another value";         z = "yet value";         bar {            ...         }     }     ... # configuration other objects }  instance2 {     foo {         x = "...";         y = "...";         z = "...";         bar {            ...         }     }     ... # configuration other objects } 

you might run application myapp.exe -cfg example.cfg -scope instance1.

the above advice provides following benefits:

  • your application can create multiple foo objects, each of can configured differently, passing different scope parameter constructor of each object.
  • users have flexibility of being able store multiple sets of configuration variables inside single configuration file, if want. example, user might have different sets of configuration variables for: (1) different unit tests; (2) development, uat , production environments; (3) multiple instances of replicated server application.
  • over time, can write library of configurable classes follow above design principle. library of configurable classes can reused across multiple applications.

i have used above approach in several c++ , java config4*-based applications wrote, , has worked me. if using language has built-in support reflection (such java), alternative approach use dependency injection framework. if don't know is, internet search "dependency injection", "inversion of control" or "spring framework".


Comments