John Lam 1997 — 2026

Code generation comments from Pieter Hintjens

Pieter Hintjens is one of the creators of GSLgen. Recently, Pieter left a very insightful comment under my code generation epiphany piece that I’d like to replicate here on my weblog where more folks are likely to read it:

John, your insights into the why of code generation are excellent. It took me a long time to come to the same conclusion, that the power came from abstraction and not reuse. By the way, let me introduce myself: I’m one of the authors of GSLgen, along with my long-time friend Jonathan Schultz.

The surprise with GSLgen is not that it works powerfully (it was very carefully designed to be powerful). The surprise is that people are surprised by it.

I wrote an article for DDJ in 1999 about GSLgen and code generation. Now, I’ve written a couple of articles for this magazine, and I know the editor, yet they found that code generation was so obscure and such a bizarre idea that they did not publish the article.

And yet, how else can you abstract your ideas up from the banal level of writing code line by painful line?

I began writing code generators with Leif Svalgaard in 1985, in COBOL, part of a huge toolkit that generated portable COBOL applications for mainframes, minis, PCs. Our first code generators were very specific: generate database code, generate forms, whatever. You can still see echoes of this in my Libero tool (www.imatix.com) which generates code for state machines. State machines are a very powerful abstraction, much loved by telecoms programmers, and Libero (which DDJ did print an article on) has been widely used since it was released in 1992 or so.

GSLgen does everything Libero does, but for an infinitely wider set of abstractions. While Libero says: “here, we’ve developed this nice abstraction, and here is a code generator that implements it”, GSLgen says “here is a way of defining and implementing arbitrary abstractions.”

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?

The combination of XML plus ‘simple scripting language’ is very potent. When that scripting language is intended for human use (not like XSLT which is potently awful to use), and aims at code generation (doing small but valuable data manglings), you get a tool like GSLgen.

Our company - iMatix - does just about everything through GSLgen. We simply don’t write code by hand any more, unless it’s GSL scripts, or XML abstractions. What does this give us? It’s hard to compare efficiency because so much depends on the actual value of the resulting applications, but we can build complex applications for about 10% of the cost of other teams. That’s a 90% saving… and about half of the remaining cost is in things like testing and building that we don’t yet automate enough. GSLs produces near perfect code, at about 1/10th the cost of writing code by hand, and it’s code that costs almost nothing to maintain. This gives you some idea of how we’re breaking the rules of traditional software engineering.

Since GSL/2 was released, we’ve continued pushing the language towards something more like a modern OOP scripting language. The first GSL syntax was truly minimalistic: just enough to work through the XML tree and produce output under various conditions. When we made GSL/1 in 1996, we’d been developing this ‘minimalistic’ language for about ten years. Again, if you know any of the iMatix tools, you’ll see echoes of it in Libero and Htmlpp. Designing a language that mixes well with code is a prerequisite for template-based code generation.

It’s not really necessary to be able to (e.g.) access databases in a code generator, but when you get used to playing with XML abstractions, you start to thing: “why not build a web server in GSL… and handle the configuration file as an abstraction…?” All it needs is socket i/o. Check. Process control. Check. Multithreading. Check. Since GSL/3, the GSLgen engine has been a multithreaded affair built using our SMT kernel. We have this habit at iMatix of making bizarrely useful things (like Xitami). And most of this technology is now going into GSLgen.

As far as we know, GSLgen is a unique thing in the software world: not so much a code generator as a way of making arbitrary code generators. As John Lam said, it takes a stubborn mind to make that worth something. But in this industry we’re faced with a fundamental problem. We keep changing the languages we use, thinking that if only we can move from C++ to Java, from VB to C#, all our problems will go away. What we forget is that each move means rewriting years of work. Throw it all out, start again. It’s a sad waste of our lives. With code generation, you can escape this cycle. Abstractions are immortal. Abstractions can live forever. Not five years, ten years, but as long as there is software running on computers. This is why Libero is still popular and highly useful more than a decade after I wrote it. Migrating your code generators to a new language is often a matter of a few days or weeks only. If you do it right (ah, grasshopper, try again) you will find yourself floating above the language wars.

It takes a stubborn mind to think of todays’ work in terms of tomorrow. But it’s worth it, and for professional programmers who value their time, it seems to be one of the few avenues open.

1 archived reader comment

Gary (2003-10-08)

Creating Code is becoming a thing of the past and I feel that GSLgen has made a huge step to this being a reality.

Question is GSL/3 available or is this in house only.

Well done