5

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?

Edson Medina
  • 214
  • 2
  • 9
  • What languages and tools are part of your project??? – Job Aug 15 '12 at 17:09
  • 1
    @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
  • 1
    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 Answers3

9

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"

KeesDijk
  • 8,918
  • 4
  • 35
  • 41
6

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.

Vijfhoek
  • 3
  • 3
Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • 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 '14 at 06:59
  • You can find a brief explaination on wikipedia (https://en.wikipedia.org/wiki/Generation_gap_(pattern)) and also some slides (http://slideplayer.com/slide/7920041/). – Fil Sep 07 '16 at 21:36
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.

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Yusubov
  • 21,328
  • 6
  • 45
  • 71