John Lam 1997 — 2026

Code generation observations

I’d like to comment on a few of Pieter Hintjen’s comments in this entry:

“This is not a freedom that many people can use. It’s the white paper problem. You may be free to draw anything you like, but it only works if you’re a capable artist. So much easier to color by numbers! Good abstractions take many years to develop and refine. Sure: interfaces, test cases, SQL… these are trivial. But how about an abstraction for a mobile phone interface that will generate the tens of thousands of lines of code that implement it? “

This one paragraph is incredibly insightful; I love the paint-by-numbers analogy! It also captures the essence of embarking on a large development project with code-generation as the core technique.

In the beginning, you need to be able to walk before you can run. Code generation is such a foreign technique to most people that they really need to wrap their minds around a concrete example before they can proceed to the next level. This is where the low hanging fruit really comes in really handy.

In multi-tier applications, the most obvious place to apply code generation is by automating the creation of the object/relational mapping code (often called the data access layer). This is tedious and error-prone code to write, and has spawned a large number of code generation articles and products that purport to solve this specific problem.

Solving this problem typically involves:

  • Generating the database schema
  • Generating the stored procedures to access the database entities
  • Generating the proxy code that calls the stored procedures
  • Generating the code that persists entity objects to the database
  • Generating the code that manages logical and physical relationships between entities
  • Generating the entity classes

Of these, generating the schema and the entity classes are the most straighforward operations. Next in line are generating the stored procedures and the proxy code that calls them. You are really forced to think abstractly when generating the code that manages the logical relationships between entities as defined by your schema. These are the classic 1:1, 1:many and many:many relationships (among others). When you implement these, you will find patterns that naturally arise from your schema, and code generation lets you capture these patterns in your application.

One book that I have found that really captures the essence of many of the patterns behind the logical relationships in your database schema is Martin Fowler’s Patterns of Enterprise Application Architecture. It doesn’t take much of a stretch to capture Fowler’s patterns using code generation (in fact he obliquely refers to implementations via code generation in the text that describes several of the patterns in his book). Once these patterns have been captured in your schema + code generator, they become building blocks for more complex abstractions that are specific to your application.

Using a tool like GSLgen requires that you capture the semantics of your entities and their relationships within a “little language”. GSLgen requires that this little language be described using XML, as this gets around the non-trivial problem of writing a parser for an arbitrary context-free grammar. Next, you write your code generator using GSLgen scripting language which provides native support for maniplating XML within the context of the scripting language. Finally you write a program using your mini-language, compile it using GSLgen and your code-generation script to create source code in your favorite programming language.

Effective use of GSLgen requires that you not view it as a “wizard”. That is, the code that it generates is not intended to be a one-shot deal. You version control the program(s) written in your mini-language and the GSLgen script(s). You NEVER version-control the generated code, and you NEVER modify the generated code.

However, generating the data access layer is just the first step, as it is largely plumbing code. The interesting parts of your application appear when you have to implement the business logic that manipulates the data access layer. This is where code generation can help, but this is also the area where the going gets tough. I’m sure that this is what Pieter meant when he said that “Good abstractions take many years to develop and refine”.

Using code generation to build your application abstractions requires a very thorough understanding of the problem domain. You need to analyze the patterns that recur in your application and build abstractions on top of those patterns. One approach that I’ve found to be very helpful is to think in terms of workflows or tasks. This lets you find the recurring patterns, and capture the variations within a pattern within your “little language” and your code generator. I suspect that there are interesting patterns that will recur in various application domains, and it is my goal to research and catalog these patterns over the next few years. With an approachable pattern and reasonable examples, I suspect it will make code generation much more approachable as a technique for solving application problems.

But wait you say, can’t your abstractions leak? Absolutely! And I would argue that they should leak. One important caveat to always remember is the risk of building an abstraction that is too complex; I can virtually guarantee that an all-encompassing abstraction that builds 100% of your code will also be so complex that it is unapproachable and unmaintainable. Instead strive to keep your abstraction as simple as possible, generating the majority of your code but then delegating to your implementation language those exceptional / arbitrary / boundary cases that are so difficult to capture using your abstraction.

The best way of seeing how code generation works is by example. To help folks get started, I’ll be posting a series of entries starting next week that walk through code generating a simple, yet highly generalizable application using GSLgen.

3 archived reader comments

Eric J. Smith (2003-04-23)

I am wondering if you are aware of an application that I wrote called CodeSmith. It is a template-based code generator similar to GSLgen. If you have taken a look, what would it take to make CodeSmith your code generator of choice? In other words, what does GSLgen have that CodeSmith doesn’t?

Eric J. Smith (2003-04-23)

Forgot to mention the URL for CodeSmith: http://www.ericjsmith.net/codesmith.htm

John Lam (2003-04-24)

I remember seeing this earlier, but didn’t look into it because I was distracted by other things. I’ll check it out and contact you separately by email. Thanks, Eric!