Consider a file. The file is a bunch of records, each with some predefined size, say 128 bits. The bits in each record hold information. For instance, bits 0-5 hold "foo" and bits 123-127 hold "bar". The records follow one another in the file in chronological order. If record B comes after record A, than "foo" had first its value in A and then its value in B, etc.

What I need to do is to decode such files, according to scenarios. The scenarios are similar in many ways, but also different in several ways. An example scenario is - look for "baz" throughout the file, and if it changed from X to Y, report something. Etc. For different scenarios, "foo", "bar", X, Y are all different, sometimes in subtle ways.

For now I have 4 scenarios, and my implementation is extremely ugly - a whole function for each scenario. There's a lot of duplication in there... Many more scenarios will be added, and I fear that my code will turn into a ball of mud. Therefore, I'm trying to think of some advanced solutions.

What I have in mind at the moment is some form of meta-programming. That is, to define some language that describes these scenarios, and write just one program that reads a "scenario", takes a file and runs the scenario on it.

This way I will have just one generic scenario running code, and multiple scenario description files, with minimal duplication between them.

This approach is very charming in theory, but hard to implement in practice. The two major problems I see at the moment are -

  1. Defining the "scenario description language". A config file ? A simplified programming language (I will need IFs and stuff...) ? This may be quite complicated.

  2. How to reflect the subtle differences between the scenarios... This is very task specific, and I fear that even if I define the language, I will have to constantly adjust it as long as scenarios with new complications appear.

I will report on how this goes. It won't be simple, but I hope I will prevail eventually. Hey, this may turn out into a complete scenario decoding system, and may even be useful for others. But there's still time until then...

Good luck to me