What is Metaprogramming?
Meta programming is writing programs that write programs. Wikipedia currently defines metaprogramming as follows:
“Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at compile time during runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually.”
There are three common ways to solve a repetitive programming problem
- Brute force - Manually write each similar piece of code
- Language extension – Write a parameterizable object, method or function to solve the problem.
- Application generation – Use a generator to create the scripts you would otherwise have manually written using brute force.
For obvious reasons (boredom, likelihood of errors and maintenance efforts), brute force is not a good idea. The question then is whether to use language extension or application generation for a given problem.
The benefit of language extension is that you don’t need the complexity of a separate application generator (or as in the case of Doug Hughes’ Reactor, an inline generator). The downsides of language extension are performance and complexity. The more you litter your application with dynamic, flexible design patterns, the more difficult it can be to follow the application logic.
Also, there is some code that is just not practical from a performance perspective to generate at runtime. For example, I wrote a set of parameterizable user controls to make it easy to specify the type of control to use to edit every attribute of their business entities (pages, users, products, articles, etc.). Back in 2003 I did a project which generated the form code from scratch with database calls to get all of the parameters for each of the field types. Then the client went and built a form with 100 fields. The performance results weren’t pretty! I looked at optimizing the data structure, writing a single stored procedure to return all of the data and caching of queries, but in the end I decided it would be better just to write a generator to generate a script containing the hard coded controls for each attribute. When I did, the form display speeded up almost 100 fold!
There are a number of great resources on metaprogramming (like this and this) but I’ll be spending most of my time looking at the subset that is application generation. Whatever approach you use it is worth learning about application generation so you know how to do it and can see when you might want to use it.


There are no comments for this entry.
[Add Comment]