Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as not constructive by gnat, Walter, Martijn Pieters, Kilian Foth, BЈовић Jan 25 '13 at 12:58

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What languages and tools are part of your project??? –  Job Aug 15 '12 at 17:09
    
@Job, I don't think the languages are relevant. Design patterns should suit any language. –  Edson Medina Aug 15 '12 at 19:25
    
Also - like I said in my post - it's a ground up project, so no tools are involved. –  Edson Medina Aug 15 '12 at 19:26
    
Edson Medina, there exist fundamentally different approaches; some are baked into compiler/IDE, such as template meta-programming for C++. The Boost library for C++ allows creating pretty powerful macros. Lisp has a macro system, which is different for C/C++ macros. These might not be enough to avoid code generation completely, but it would be unwise to ignore this stuff. VS2010 comes with T4 which is also very cool. "Code generation" is too generic - you might want to generate an enum from a live database table or convert MSFT SQL to Oracle or convert a DSL to other language. Details matter! –  Job Aug 15 '12 at 19:37

3 Answers 3

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"

share|improve this answer

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.

share|improve this answer
    
Nice one! I've seen that pattern before (and I've been using it) but I didn't know its name. Thank you! –  Edson Medina Aug 15 '12 at 19:30
    
The link is down! –  Mihai Jun 23 at 6:59

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.