Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So I have been looking through some code I wrote a few years ago for an economic simulation program. Each simulation has a large number of settings that can be saved to a file and later loaded back into the program to re-run the same/similar simulation. Some of the settings are optional or depend on what is being simulated.

The code to read back the parameters is basically one very large switch statement (with a few nested switch statements). I was wondering if there is a better way to handle this situation.

One line of the settings file might look like this:

#RA:1,MT:DiscriminatoryPriceKDoubleAuction,OF:Demo Output.csv,QM:100,NT:5000,KP:0.5 //continues...

And some of the code that would read that line:

switch( Character.toUpperCase( s.charAt(0) ) )
{
    case 'R':
         randSeed = Integer.valueOf( s.substring(3).trim() );
         break;
    case 'M':
          marketType = s.substring(3).trim();
          System.err.println("MarketType: " + marketType);
          break;
    case 'O':
          outputFileName = s.substring(3).trim() ;
          break;
    case 'Q':
          quantityOfMarkets = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'N':
          maxTradesPerRound = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'K':
          kParameter = Float.valueOf( s.substring(3).trim() );
          break;
 // continues...
}
share|improve this question
3  
Reinvent the wheel much? I suggest you use one of many existing formats for storing settings and then use a library. If you still decide against it, why not just parse a line into a dictionary first and then process it later? These lines are not very different from a complex combination of command line switches, if you remove commas, colons, and prefix options with a'-'. Ideally your program can take args on command line or read them from a settings file and work the same way. *nix tools do this. Check out stackoverflow.com/questions/491595/… – Job Aug 15 '11 at 3:42

3 Answers

From a code perspective, the cleanest method would be to switch to the Properties class. It's made for handling this, although you would either need to define all of your settings (even the unused ones) in the file it reads from or have predefined constants in a source file that you can use if the properties file returns null for a particular named property.

If you can't change the scheme, you could follow the general strategy of having a Hashtable, which is really what the Properties class does - it reads a file into a Hashtable conveniently. It would pretty much be rolling your own properties. Again, you would have to have preset values defined somehow to handle cases where a setting that you need might not exist in the file.

The way you would use either is to simply query the data structure any time you needed a value. You can either enforce that it exists in the data structure (throw an Exception and terminate it it doesn't) or define default values somehow (probably in a source file).

share|improve this answer

an excellent library for this sort of work is Apache Commons Configuration

share|improve this answer
yes properties is only string but this is numbers, dates and from different formats. neat – tgkprog Apr 4 at 18:40

Have you thought about serializing the object to disk, and deserializing it later?

share|improve this answer
2  
If you do that, you also have to write a whole tool suite to edit the configuration. I know some people who think this is a good idea, but I'll happily admit that I'd rather have a text file (or, if really necessary, XML). – Donal Fellows Aug 15 '11 at 12:30
Editing text files to change configurations is sooo 1965; in the long term, it's a maintenance nightmare which you'll end up handling with a toolsuite anyways, so might as well save th headache. The op only talked about loading and saving, so I assumed no manual changes. I generalize, of course. There are exceptions where text file config works well enough – blueberryfields Aug 15 '11 at 21:39
1  
I hadn't thought of that. While I do like the idea of being able to make a change directly in the configuration file, I'm not sure how necessary it really is. Since all of the parameters can be changed in the GUI after the file is loaded, no tools suite would be needed in this specific case. – jawilmont Aug 16 '11 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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