I am currently reading an excellent book that discusses useful approaches and paradigms when developing software called:
'The Pragmatic Programmer: From Journey Man to Master by Andy Hunt and Dave Thomas'
And in the chapter discussing the Don't Repeat Yourself (DRY) principle it mentions the ingenious use of code generators to avoid duplication during the development process.
Has anybody used T4 Text Templates in dot NET to generate code and other files? Has anybody used code generators in other languages to minimise duplication? One use case involved generating code from Business requirements documents, ensuring that the requirements model always aligned with the code as requirements changed.
What are your thoughts on code generators and their usefulness?
I think code generation can be useful if exactly one of these holds:
If the code may be regenerated but is also editable by humans, it immediately becomes a maintenance nightmare. Someone will have changed something in the resulting code, someone else changes something in the generation process, and you have a hard to find bug.
This is perhaps the most useful case. Some examples:
Templates, macros, generics. Can be hard because especially the former lack type safety, and error messages may relate to the result instead of the transformation code. But very powerful.
Languages differ greatly in how they handle this (and whether they even use it at all, or use reflection instead, or nothing). See this question.
Ideally I'd want these to happen at compile time, with the resulting code never exposed to the programmer, because it shouldn't be changed.
Here you generate code that you then edit and becomes part of the project.
This can be used to generate the structure and boilerplate of your project, that you then adapt. It can save some effort and help keep the code layout consistent. E.g. many frameworks come with a command to create an empty project.
Where possible, it's better not to need the boilerplate. But there'll be some, so this has a place.