views:

259

answers:

6

How would you identify and fix the following code smell:

I've got a small scientific computing app that I'm writing that has to be able to handle lots of variations on the same theme. The inner workings of it are well-factored, mostly using the template method pattern and some higher-order functions. However, specifying how all these classes and functions should be instantiated and used and with what parameters in any given run of the program is so complicated that I sometimes think the easiest way to do it would be to rewrite main() for every run and recompile it.

Is there a relatively simple, lightweight way of doing configuration management that's not overkill for a small scientific app? Basically I've been just using command line switches and they're getting pretty unwieldy.

Edit: The app is small enough that build times are negligible. I see little advantage in binding to a scripting language over simply modifying the code in the native language. (The app is written in the D programming language.)

Edit # 2: I thought of the config file idea and it would help, but I feel like writing the config file would be almost as hard as rewriting main() every time.

+5  A: 

From a design pattern perspective, the Builder pattern may be useful to you. It will allow you to define some good default configurations while allowing you to override particular steps. It works particularly well with a Fluent Interface.

The Facade pattern also comes to mind, because it allows you to pre-define common configuration options. You could use Builders to implement the Facades.

Mark Seemann
A fluent interface would still require him to code and recompile in order to change the configuration, isn't this part of the problem he's trying to get rid of?
Richard Ev
Honestly, I can't tell from the question. If recompilation is the issue, then you are correct. If the issue is related to maintainability, a Fluent Interface might help...
Mark Seemann
A: 

Sounds like you need to give the core code a binding in your favourite scripting language, and then use that language to build configurations.

Andrew McGregor
+3  A: 

Sounds like the best thing to do is to embed a scripting language and use that to drive your app. Scripting languages makes great 'config' files. I'd suggest looking into lua (widely used in games) and tcl (widely used in electronics CAD). Both are easily embeddable and small and easy to learn.

slebetman
A: 

If you can give names for each set of settings, you can just pass the names to your main().

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

For example when you select Red Army in strategy game, there may be 1000s of parameters for each army but they are all set for something that would represent Red Army.

Serkan
A: 

You could look into some scripting language like Python and call that from main().

Another option would be to create your own DSL (Domain Specific Language) for your app. You can use ANTLR for this.

erikkallen
+1  A: 

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

I'd recommend a little additional work to create an example configuration object and serialize that and edit it to give you a template configuration to work with.

Richard Ev