|
Programs that write programs
Following pointers from Ned Batchelder's recent excursion into code generation led me to another nice example of the power of dynamic languages. In order to streamline his use of C++, Ned wrote a little tool called cog which enables him to embed, in C++ programs, Python fragments that generate verbose and/or repetitive C++ constructs. He adds:
For more about code generation in general, try:
-
Dave Thomas interviewed about code generation. Dave Thomas is one of the Pragmatic Programmers, and I find I agree with him almost universally. He forbids putting the output of code generators under source control, I encourage it. We agree that the output should never be edited.
- The Code Generation page on the c2 wiki. As will happen with a wiki, this fractures off in many directions, with many different viewpoints, both for and against code generation.
[Ned Batchelder]
In the interview Ned cites, Dave Thomas gives an example of a Ruby feature that I've heard of, but never had occasion to use. In a class definition you can write Ruby code to define a type. That means, as Thomas puts it, that "you can effectively extend the language at runtime from within." Statements like that have a tendency to alienate people. It can sound like the drug-induced fantasy of some idealistic tree-hugging Birkenstock-wearer who isn't living in the real world of Enterprise Software Development. But Thomas backs it up with a great example. In this case, he used Ruby's dynamic extensibility to wrap a database schema in classes that can either persist objects to the database, or create schema documentation, depending on how the methods that dynamically define those classes are defined.
Once upon a time Tom Christiansen gave me a great quote, which he attributes to Andrew Hume: "Programs that write programs are the happiest programs in the world." Templating and code generation are examples of this happy strategy. We've always known that dynamic languages are a great way to create "little languages" for specific tasks. But we don't yet fully appreciate that all programming is a continuous process of language invention. And we don't (yet) evaluate programming-language productivity on those terms. Dave Thomas:
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.
We are linguistic animals endowed with a protean ability to generate language. Naturally we'll want that same generative power in our programming languages.
|