Sunday, March 4, 2007

Don't generate

Last week I've spent quite some time debugging a web service and its corresponding client code. The client code was generated from the web service description (WSDL). I'm sure the generated client provides a wonderfully easy way to access a web service from your code, but it didn't help to make my debugging effort any easier. Code generators have the habit on not focusing on maintainability of their generated offspring, let alone that they care about the sanity of their designated debugger. In the end I'm pretty sure I got all problems lurking in there, but it reminded me of one of my older principles: don't generate code from data.

If you've written code to interpret some data and then generate code from it, why not skip the code generation step? Just keep the code that interprets the data and work from that. That means that as soon as you update the data, your program will work with it. No forgetting to generate the updated code, no risk of overwriting handwritten changes (see the "generation gap" pattern).

Some might find this approach less flexible and not as easily supported by tools (although that is changing with the current push for DSLs), but I find it a lot easier to control... and debug.

No comments: