Code Generation, Served in a Lightweight Process
So, why another code generation tool? If you search for code generators on SourceForge you'll find 897 tools. Surely, out of all these projects there's one that would meet my needs.
Unfortunately, once you sort out all the Java tools, tools solving specific and limited code generation problems (e.g. tools that only generate data access layers), and projects that never got beyond the planning stage, there are very few general purpose code generators for the .NET platform. "Okay," some will say, "but there are tools, right? How many do you need?"
Only one. But here's the final sticking point, I want a tool that fits well into a lightweight iterative development methodology. For me this means:
Unfortunately, once you sort out all the Java tools, tools solving specific and limited code generation problems (e.g. tools that only generate data access layers), and projects that never got beyond the planning stage, there are very few general purpose code generators for the .NET platform. "Okay," some will say, "but there are tools, right? How many do you need?"
Only one. But here's the final sticking point, I want a tool that fits well into a lightweight iterative development methodology. For me this means:
- Readable templates. I expect to refactor the templates frequently during the development process. Readability supports maintainability.
- Fits well into a continuous build environment. Code generation should be just another task in the build process. Modify the templates, start the build, and away you go.
- No specific tool dependencies. Let me develop my templates in my favorite IDE. I'll be more efficient if I'm not switching back and forth between tools.
Here are the development steps:
- Test-first develop some classes that seem to fit the requirements.
- If these classes seem amenable to code generation (more on what I mean by this in another blog-ette), then use the NGraver tool to transform this sample code into starter templates.
- Test-first develop the templates so that they produce the desired code. Presumably, the more well-factored the code is to begin with, the simpler the transformation code will be.
- Refactor the templates as needed.
Ward Cunningham, a leading light in the XP movement, summarized these steps very nicely in a recent email:
- Be concrete
- Reflect
- Wield meta-power
- Consolidate
This could be the NGraver mantra.