Odor Code: Configuration Nightmare

How do you identify and correct the smell of code:

I have a small scientific computing application that I am writing to be able to handle many variations of the same topic. His internal works are well taken into account, mainly using the template method template and some functions of a higher order. However, indicating how all these classes and functions should be created and used and with what parameters in any given program start is so difficult that I sometimes think that the easiest way to do this is to rewrite main () for each start and recompile it.

Is there a relatively simple and easy way to manage configuration that does not overwhelm a small scientific application? Basically, I just used command line switches and they get pretty bulky.

Edit: The application is small enough that the build time is negligible. I see a slight advantage in binding to the scripting language by simply changing the code in my native language. (The application is written in the programming language D.)

Edit # 2: I was thinking about the idea of ​​a configuration file, and that would help, but I feel that writing a configuration file would be almost as complicated as rewriting main () every time.

+7
design-patterns configuration code-smell scientific-computing
source share
6 answers

From a design point of view, the Builder template may be useful to you. This will allow you to define some good default configurations, allowing you to override certain steps. It works great with the Fluent Interface .

The Facade pattern also comes to mind as it allows you to predefine common configuration parameters. You can use Builders to implement Facades.

+7
source share

It seems like it's best to embed a scripting language and use it to control your application. Scripting languages ​​make excellent "config" files. I would suggest looking at lua (widely used in games) and tcl (widely used in CAD electronics). Both are easy to integrate and are small and easy to learn.

+3
source share

If D supports object serialization, then why not create a class that represents your configuration (it can appear as one class with many properties or several classes that are aggregated by the parent class) and serialize / deserialize it?

I would recommend a little extra work to create an example of a configuration object and serialize this and edit it to give you a template configuration for the job.

+1
source share

It looks like you need to point the main code to your favorite scripting language, and then use that language to create the configurations.

0
source share

If you can specify names for each set of parameters, you can simply pass the names to your main ().

Even if you have hundreds of parameters that you can set, in fact, how many combinations do you really use at the end? If you use, let them say that 4 combinations give each of them names, and you can even keep them hardcoded as different sets.

For example, when you select the Red Army in a strategy game, there may be 1000 parameters for each army, but all of them are configured to represent the Red Army.

0
source share

You can look at a scripting language like Python and call it from main ().

Another option is to create your own DSL language for your application. You can use ANTLR for this.

0
source share

All Articles