What's the use of metaprogramming?

Viewed 19349
11 Answers

I'll try to explain my concrete example of using meta programming techniques.

I've created a program tool which will generate ASP.NET web page source code from any MS Access data entry form. The technique that I used was to create my own ASP.NET text templates for each type of form control. I simply plugged in the values such as TOP, LEFT, HEIGHT, WIDTH, CONTROLSOURCE from the MS Access form objects meta data. For example, my template for an ASP.NET text box looks like this:

 <asp:TextBox ID="**ID**" runat="server" style="z-index: 1; left: **LL**px; top: **TOP**px; position: absolute"  Text='<%# Bind("[**CTLSOURCE**]") %>' />

after getting the textbox control meta data values, my program generates the code for the text box

<asp:TextBox ID="txtCustomerID" runat="server" style="z-index: 1; left: 50px; top: 240px; position: absolute"  Text='<%# Bind("[CustomerID]") %>' />

My program generates the entire web page source code for one MS Access form in 2-3 seconds.The alternative is to code by hand the ASP.NET web page from scratch; a task that could potentially take hours or even days.

Imagine an MS Access database with 24-35 forms. To hand code each and every form as an ASP.NET web page source code could take weeks if not months. Using a conversion tool with meta programming techniques , in this case, reduces development time for the web pages from weeks and months to hours .

We use meta-programming a lot to create properties in VBA. We have various Excel spreadsheets with many headers on them and we want to define getter/setter properties for each header, allowing us to manipulate cells under that header. Manually doing this would be a nightmare.

The meta programming framework of choice for us was Notepad++ and its find/replace regular expressions capabilities. Here is how we meta-programmed our properties:

  • Copy a list of headers from Excel to Notepad++
  • Record a Notepad++ macro to clean up the data (remove whitespaces and special characters). At the end of this we have a list of newline separated strings.
  • Manually copy the list to another .CSV file and use Excel to generate a list of line numbers. Then copy back to Notepad++.
  • Write a regex to convert a property name into a property definition, adding all the whitespace, keywords etc. Use the line number as the column number in our property definition.

At the end of this we have a process that's a mixture of manual steps, recorded macros and a regex that we can re-apply every time we want properties for a sheet. And we did! To great effect.

That's the power of meta-programming. When to use it is a matter of experience/intuition. But I recommend answering this question:

Will if be quicker for me to just code this directly, or can I automate some/all of the process, and speed up my process?

That gives you a line to draw beyond which meta-programming is no longer useful. If you can just code it quicker, even if it's 10 repetitions, just do it! Only if it's hundreds of repetitions, or it's something you expect to reuse many times in future then meta program it.

Another point is that there are degrees here. I once wrote a Java program to create a bunch of files for adding a new IntelliJ inspection to an inspections coding project. That was a fair bit of overhead: creating the Java project and compiling it etc. On the other hand, Notepad++ find/replace is just a tiny step above manually typing stuff yourself. The advice here is to start doing things manually and then automate as you see a need, only up to the point where it makes sense. No need for a Java program when Notepad++ will do. No need for Notepad++ when manually typing it will do.

Related