I’m working on a ground up code generator project, but I feel I might be reinventing the wheel.
Does anyone know where I can find some best practices guidelines or design patterns for code generation?
4
Real best practices are hard to find because there is still a lot discussion on good code generation. What level of abstraction do you model, what is a model, do you use a graphical or a textual model, internal or external DSL on what level do you want to use code generation (from simple developer help to complete software factory)
One of the best books out there is “Domain specific modeling” Although if you are in the microsoft space it might be a bit difficult to map on your own projects.
Also the DSL book by Martin Fowler can give you some good ideas on the terminology and idea’s behind DSL’s and how to use the for code generation.
If you are looking for language workbenches the language workbench challenge is a good place to see what is out there and what other people are doing.
Hope this helps.
extra: I also read “code generation in .net” the technology described is old and also just a “how to” definitely no help in best practice or design patterns
25-1-2013 EDIT : A new book was just released that might be very helpful : “DSL Engineering – Designing, Implementing and Using Domain-Specific Languages”
For making generated code more maintainable, you may be interested in the Generation Gap pattern. This is one pattern that didn’t make it into the GoF book, but that John always liked and later described separately in the same format.
3
It depends on project
Your tools and practices will vary depending on what specifically you have to do for the project. For example: in ASP.NET MVC and .NET development you may consider T4 Text Templates
. Basically, what it does is usage of the pre-build and custom templates to scaffold a model for generation of the related files (like add Controller and views).
Nice blog has a quick intro T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
However, you may also use T4 in different code generation processes as well.