Tom Hollander just posted a note Code Generators: Can't live with them, can't live without them. His question is really about the ROI on developing code generators. Most of the comments, however, are about the specific case he is trying to address with generators; creation of database mapping code.

Let me try to address Tom's key question: When it is worth creating a code generator as opposed to either (a) writing the code by hand or (b) trying to bake the logic (in this case db mapping) into a framework?

My experience with writing code generators is that their complexity grows very fast. Simple code generators, like the ones based on T4 templates used in GAX and DSL designers, are relatively inexpensive. However they suffer from the issues Tom outlines like, how to separate the generated code and the hand-written code, or how to generate and regenerated only the code that you need? 

As soon as you attempt to address those issue, the complexity of the generator sky rockets. Take the latest version of the Web Service Software Factory as an example. The code is selectively generated from models. You can generate code only for selected model elements, the entire model, or even connected models. However to do that, we had to create a code generation framework that extends the out-of-the-box T4 template-based code generation of the DSL toolkit. We also had to add a model validation framework to ensure that the models are consistent before we attempt to generate the code. Developing those mechanisms was a significant portion of the cost of building the factory. Our estimate, which we presented at the last p&p Summit, was that we spent over 50% of the effort on building the factory infrastructure, which did not include writing templates, code generation strategies, etc.

As another example take the LINQ-to-SQL modeling tool. It selectively generates LINQ code, but to do that it introduces a model that sits between the database and the code generators.

Bottom line, unless simple code generator addresses your needs, think very hard about the cost of building a more sophisticated generator. Almost always you end up introducing an explicit or implicit model of what you are generating or the generation mappings. However if you really need to do that, take a look at what we have done in the Web Service Software Factory and see if you can reuse the mechanisms we developed.

Wojtek