Code generation vs data driven programming
Via Ned Batchelder, this interview with pragmatic Dave Thomas on code generation closely reflects my own nascent thoughts on the issue:
CGN: What do think the future is for code generation?
Dave: I think that in the long term the larger code generation efforts, the “application generators,” will become a thing of the past. They are there because the underlying technologies and architectures don’t yet support programming at a high level. But I’m betting that languages such as Java and C++ will in the long term be seen as a curious branch in the evolution of computing. I’m hoping that somewhere out there some bright spark is coming up with a way of letting us write applications expressively and dynamically. Once this happens, the need for these kinds of code generators will diminish.
For example, I rarely (if ever) write a code generator that generates Ruby code: there’s just no need, as Ruby is dynamic enough to let be do what I want without leaving the language.
In the shorter term, though, I think code generators of all kinds will continue to contribute significantly to the industry. Java and C# are both such stifling languages that you need to be able to use code generators to make them effective.
We considered using code generators for our current major project at work, and picked up Jack Herrington’s book on the subject. Reading through it, it became clear that many of the problems that code generators solve can be tackled instead using data driven programming techniques made possible by dynamic languages. Since we had already settled on Python as our implementation language the need for code generation became far less apparent, and we ended up avoiding it entirely with the exception of a command line tool for passvely generating basic templates for our admin interface.
If I ever have to work with a less expressive language I’ll certainly consider using a code generator (probably written in Python) to abstract away some some of the tedious repetition. As it is, Python’s rich data structures and clean support for introspection provide an excellent alternative.
I certainly agree, and always go out of my way to avoid code generation. Besides using a dynamic/expressive language, you can avoid code generation by avoiding boilerplate. Eventually the boilerplate will annoy a programmer enough to make a template, and from there it's only a small step to more substantial code generation.
The other side of the issue is "what is code". Where does HTML generation fall in this? Or Javascript generation? Who among us hasn't generated Javascript? Is fully dynamic generation okay, as opposed to static generation? Is it only okay when you never edit the code? HTML form templates? What about code templates? Project templates? Copyright headers? Well, now I'm just getting pedantic...
Ian Bicking - 11th February 2004 19:19 - #
rick - 12th February 2004 05:59 - #
Simon Willison - 12th February 2004 07:04 - #
When Jon Udell quoted from the same interview with Dave Thomas, he penned a perfect description of Lisp, the mother of dynamic languages.
Paul Graham calls it bottom-up programming in On Lisp. Lisp macros are code generators that are baked into the language. Lisp's secret power is the ease with which it can be extended into custom "little languages" which match the domain of the program under development. But code generation is only a part of that power. Functions and lexical closures and data-driven, functional programming are also very powerful tools.
My dynamic language of choice these days is perl though I'm also a fan of python. I suspect that perl and python and ruby are all interesting precisely to the extent that they have copied lisp power tools.
It's not really a question of whether to use code generation, but when. Paul Graham's answer to that question is "only when nothing else will do." But as he demonstrates clearly throughout On Lisp, there are problems for which lisp macros are the only solution and they create very powerful tools.
Parahprased from my response Lisp blazed the trail, (code generation and dynamic languages)
Eric Dobbs - 12th February 2004 16:37 - #
Mean Dean - 12th February 2004 18:37 - #
Girts Kalnins - 8th December 2004 21:55 - #
jimX0r - 3rd March 2006 11:07 - #