I spent some time this evening googling to see who else is talking about code generation.
This first article by Matt Stephens is a very good overview of the problem space. He motivates the problem well, and motivates why code generation is a good fit. In particular, he spends tie talking about generating the data access layer of an application, an area that I often see as being some of the “low hanging fruit” for code generation. Unfortunately, Matt doesn’t provide any concrete examples of code generation in action, which makes his paper feels a bit too abstract.
Next, I found an article that talks about generating code using XSLT. Jeff Ryan walks through a fairly complete sample where he builds a JavaBean component using XML and XSLT. Christian Georgescu wrote a similar article in the C/C++ Users Journal where he talks about generating C++ code from XML and XSLT. I like Christian’s motivation part of his article.
Based on my experience, writing a code generator in XSLT is difficult since the syntax of XSLT tends to obscure what the generated code looks like. Maintaining an XSLT-based code generator would be difficult for non-trivial applications.
The thing that I really like about gslgen is that the syntax of the code generator is very clean and simplistic. Over the next week or so, I’ll publish a few sample code generation scripts using gslgen to illustrate the utility of the technique.
5 archived reader comments
Jason Kaczor (2003-03-24)
I spent 8 months last year working on code-generation for a large-scale system, essentially all “business entities”, their data access layers, inheritence chains and database infrastructure was all generated from XML source models. If I only had more time, I would have been able to generate from UML models directly, rather than XMI exports. Oh well.
Dave Lee (2003-03-25)
for an XSLT alternative, check out Caucho’s ‘StyleScript’. It has some obvious non-technical limitations/questions (‘Caucho Developer Source License’), but definitely improves upon many XSLT pains. Links:StyleScript docs & examples and more StyleScript docs & examples
Rob Walker (2003-03-26)
I am working on a project at the moment that has a large code generation phase at runtime not compile time. I’ve been playing with gslgen after seeing a reference to it on your site last year. It is a great tool, but is getting too limited for more complex cases. I would really like the ability to use XPath queries to select other parts of my input, and template matching based on some attributes of the current node rather than using large ‘if’ statements. I had thought about using XSLT, but don’t like the syntatic complexity. Looks like the best approach will be to implement something like StyleScript or XSLScript (http://www.pault.com/pault/prod/XSLScript) but for a .NET based environment. Simple syntatic sugar should be enough to make XSLT palatable for what I want. Now I just have to find time for it…
Anonymous (2003-05-09)
I do not like idea maintaining source information in XML - hard to read, hard to write and hard to compare different versions. I prefer holding information in database (and simple gui tool to maintain it) and use SQL scripts to generate code. Something like this simplistic example: select ‘drop table ’ ||
TABLE_NAME|| fromUSER_TABLESCelexa (2004-10-25)
Check Celexa info.