My flight to Orlando, FL earlier this week took me through NYC and inspired an epiphany; this entry describes that epiphany.
Over the past week or so, I’ve been spending a lot of time thinkingabout code generation. Michael Lenaghan, who has been one of my most important mentors since we met in 1995, recently got me thinking about code generation. Chris Sells, who actually created a code generation product, was unable to convince me of the importance of code generation. Why did one of my friends succeed where the other failed?
It has everything to do with how they motivated the need for code generation. Chris’ motivation was that code generation was capable of capturing knowledge for the purpose of reusing it in different contexts. Michael’s motivation was that code generation was capable of building abstractions that were not possible using traditional techniques. The problem with Chris’ motivation was that this wasn’t a problem that I encountered in my day-to-day work. I rarely had code that I needed to reuse in different contexts. However, I oftenrun into the scenario that Michael described: there are simply limits to my ability to understand problems using traditional programming techniques. I needed to use a more powerful tool.
These two motivational approaches remind me of how OOP is often motivated. Some folks motivate OOP as a code reuse technology, others motivate OOP as a mechanism for building abstractions. I think that today, most OOP practitioners would agree that it is really about building abstractions that let you visualize the problem more clearly. Not many people would claim today that they have seen significant code reuse opportunities arising out of their use of OOP.
Today, I strongly believe that code generation is not a code reuse technology. Instead, it is a very powerful tool that lets you think about software from a higher level of abstraction. But how does it let you think “at a higher level of abstraction”? This is the contribution that airplanes bring to the story.
We had fairly clear skies over NYC, so the descent into the NYC region allowed me to think about the parallels that lie between altitude and abstraction. From a fairly high altitude, I was able to see the high-level structures of the city. I could see the Hudson river, I could make out Long Island in the distance, I could see the outline of Manhattan, but I could see little else.
As we descended, I noticed that there was a change in the amount of order that I saw. Structures that were previously very ordered when viewed at a higher altitude became less ordered upon closer inspection. Also, I noticed that order that previously wasn’t observable became apparent, and then disappeared into apparent randomness as we drew closer.
An important question is: why did order “appear” and then “disappear”? This observation stems from a well-studied concept in psychology: the 7 +/- 2 rule. Simply put, it is the amount of information that can be stored in your short term memory. The disappearance of “order” corresponds to hitting the limit imposed by 7 +/- 2; the elements in the scene become too complex to process using your short term memory.
The same principles are at work when you look at software. When methods get too long, or attempt to do too many things, you lose the ability to understand their operation. When classes get too complex, again you lose the ability to comprehend their implementation. Patterns, OOP, and structured programming are all techniques that we use to create the abstractions that we use in our programs.
But is it enough? As most of you reading this have experienced, there are limits to what you can do with Patterns, OOP, and structured programming. You find that there are features that cut across your well-designed class hierarchies and object graphs. These are the warts in your program that spring to life innocently enough, but then spread as your program becomes increasingly more complex. It is these warts that make it hard for you to comprehend your software. It is these warts that make it hard for you to extend your software’s functionality.
So is code generation the answer to all problems? Of course not. However, it is a tool that works exceedingly well in certain scenarios. One scenario that I have a lot of experience in is building metadata parsers. This is a specific instance of the more general class of problems that arise when building parsers and translators.
How does my code generator work? There are two different parts: the “mini language” and the code generator. The mini language defines the solution to the problem (in my case a parser that parses metadata tables). The code generator is the glue that converts my mini language into C# source code.
I chose not to build my own code generator (which, as a long-time C++ programmer, required supression of my natural instincts). Instead, I used an obscure, open-source code generator called GSLgen. GSLgen is not a particularly great code generator, but it had one important characteristic: it was good enough. GSLgen is a surprisingly powerful blend of a primitive scripting language with a rudimentary XML parser. You define your mini language using XML (to avoid having to write a parser) and you implement your code generator using their mini-scripting language / XML parser.
During my code generation experiments, I was able to condense approximately 8000 lines of hand-written C# code into about 600 lines of a custom XML language that was in turn fed to about 500 lines of scripting code that generated a functionally equivalent parser. While I was writing this software, I was continuously amazed by the subtle semantics that kept popping up while I was defining my custom XML language. This led to an iterative development process where I would see a new abstraction, modify my language, tweak my code generator, and then test the results of the change using traditional tools (in my case Visual Studio .NET).
Why did I do all of this? Because it turned me into a vastly better developer. Code generation let me see the problem space from a much higher level of abstraction than I had seen before. Not only was my code-generated metadata parser functionally equivalent to my original hand-written parser, it had fewer bugs, it was faster, and most important of all, I developed it quicker than my original parser.
Is code generation without its own warts? Of course not. Writing code generators and defining mini-languages is hard. It requires someone who has the ability to think very abstractly about problems, and who has the patience to see it through. Particularly at the beginning when one is not sure whether the long-term results are worth all of the short-term pain.
Personally, I’m very excited about the prospects of using code generation in the software that I create. It is a new and extremely powerful tool in my toolbox. Hopefully this article will inspire you to think about adding code generation to your toolbox. But don’t just believe me, read this article next.
1 archived reader comment
Nick Franceschina (2004-01-26)
This is very interesting idea (almost obvious, after you think about it)… but after looking at GSLgen, it seems that I could do all the same things with XSL… I could do my model in XML and use XSL to transform it into any type of file I want. I see a few tiny things that GSLgen adds that XSL doesn’t (out of the box), but it would be rather simple to build in the necessary functionality… plus, XSL seems to have much more power than GSLgen.
Thoughts?