What's the use of metaprogramming?

Language AgnosticCode GenerationMetaprogramming

Language Agnostic Problem Overview


I've read:

and I confess some confusion at the purpose behind metaprogramming/code generation.

Does anyone have a concrete example of where they use metaprogramming/code generation? Even better would be an accompanying explanation of why it was better than an alternative.

edit: Would Thistle be considered metaprogramming?

Language Agnostic Solutions


Solution 1 - Language Agnostic

Imagine a guy who builds cars. Say it's the same thing as using a computer.
At some point he realizes he's always doing the same thing, more or less.
So he builds factories to build cars, and it's much better. He's now programming !
Nevertheless, once again, at some point, he realizes he's always doing the same thing, to some extent.
Now he decides to build factories that build factories that build cars. That's metaprogramming.

Metaprogramming is immensely powerful, but one glitch in the system makes all advantages turn into monster difficulties. So master it and use it... Or stay away !

Solution 2 - Language Agnostic

I think of metaprogamming as "programs that write (or modify) other programs". (Another answer said "factories that make factories", nice analogy).

People find all sorts of uses for this: customizing applications, generating boilerplate code, optimizing a program for special circumstances, implementing DSLs, inserting code to handle orthogonal design issues ("aspects") ...

What's remarkable is how many different mechanisms have been invented to do this piecemeal: text-templates, macros, preprocessor conditionals, generics, C++-templates, aspects, reflection,... And usually some of these mechanisms are built into some languages, and other mechanisms into other languages, and most languages have no metaprogramming support at all. This scatter-shot distribution of capabilities means that you might be able to do some kinds of metaprogramming in one language, with limitations, and yet not be able to do those kinds in another. That's aggravating :-}

An observation that I have been following to the hilt is that one can build generic metaprogramming machinery that works with any language in the form of http://www.semanticdesigns.com/Products/DMS/ProgramTransformation.html">program transformations. A program transformation is a parameterized pattern: "if you see this syntax, replace it by that syntax".

One transformation by itself generally isn't impressive, but dozens or hundreds can make spectacular changes to code. Because (sophisticated) program transformations can in effect simulate a Turing machine, they can carry out arbitrary code changes, including all those point-wise techniques you find scatter-shotted about.

A tool that accepts language definitions. language-specific transformations and generates another to apply those transformations is a meta-metaprogramming tool: a program to write "programs that write programs".

The value is that you can apply such tool to carry out wide varieties of changes to arbitrary code. And, you don't need the language design committee to realize that you want a particular kind of metaprogramming support, and hurry up to provide it so you can get on with your job today.

An interesting lesson is that such machinery needs strong program analysis (symbol tables, control and data flow analysis, etc.) support to help it focus on where problems are in the code, so that metaprogramming machinery can do something at that point (a very weak kind of example of this are point-cut specifications in aspects, that say "make changes at places that look like this").

The OP asked for specific examples of where metaprogramming was applied. We've used our "meta"-metaprogramming tool (http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html">DMS Software Reengineering Toolkit) to carry out the following activities on large code bases automatically:

  • Language Migration
  • Implementing Test Coverage and Profilers
  • Implementing Clone Detection
  • Massive architecture reengineering
  • Code generation for factory control
  • SOAization of embedded network controllers
  • Architecture extraction for mainframe software
  • Generation of vector SIMD instructions from array computations
  • Reverse engineering of code back to concepts

across many languages, including Java, C#, C++, PHP, ...

The OP also asked, "Why was this better than the alternative?" The answer has to do with scale, time, and accuracy.

For large applications, the sheer size of the code base means you don't have the resources or the time to make such analyses or changes by hand.

For code generation or optimization tasks, you might be able to do it by hand, but the tools can do it much faster and more accurately.

In essence, these tools do what human beings simply cannot.

It is worth noting that the tools have no creativity; you still need humans to determine what to have them do, e.g., to decide what the task is (see above list for examples) and determine how to define the analyses/transformations to achieve the effect. You still need meta-programmers. However, when a meta programmer arms such a tool with the right knowledge, the resulting code can appear to be built by an incredibly fast, creative, expert coder.

Solution 3 - Language Agnostic

I've gotten the most use out of metaprogramming for bridging between different APIs.

A working example would be FireBreaths JSAPIAuto1 that eases writing C++ classes that are exposed to JavaScript. By providing a registering facility for the functions that are to be exposed, the argument types can be inspected and from that fitting code generated at compile-time that converts from the script-API-types to native C++ types and back, even directly supporting map, vector, etc.

As a simple example, consider an exposed add(a, b) function that uses some scripting API types:

ScriptVariant add(const std::vector<ScriptVariant>& values) {
    // have to check argument count
    if(values.size() != 2)
        throw script_error("wrong number of arguments");

    try {
        // have to convert from scripting-API types
        long a = values[0].convert_cast<long>();
        long b = values[0].convert_cast<long>();
        return a+b; // potentially need to convert back too
    } catch(ScriptVariant::bad_cast& e) {
        // need to handle conversion failure
        throw script_error("conversion failed :(");
    }
}

The actual logic buried in there is only one line, that checks and conversions are annoying and redundant. With the previously mentioned registration-facility (e.g. in the constructor):

registerMethod("add", make_method(this, &MyClass::add));

this can now simply be written as:

long add(long a, long b) {
    return a+b;
}

... and the framework takes care of generating the neccessary code for you.

1: Although i would do implementation a bit... cleaner... if i would have to start again

Solution 4 - Language Agnostic

My recent (last 6 months) concrete example of code generation:

  1. I have an SQL Plus script that generates and then executes other SQL Plus scripts. The generates script runs queries against some tables that have time-stamp fields, and when I designed the script, it was impossible to know what time window to select. So, the main script does its work, and figures out what time ranges need to be in the sub scripts. Then it generates the subscripts by writing their code to file (and substituting placeholders for the actual start and end times). Finally it executes the subscript(s). I've used this trick for a few situations now (though often more complicated than this one) where the structure of the substeps depends on results of earlier steps.

  2. I once got a spreadsheet mapping elements from an XSD to table columns in a database. It was possible to generate XSL snippets and complete queries from the spreadsheet using macros and VBA. These snippets and queries were copied and pasted (mostly as-is with no neede changes) into the system that executed them and processed the results. Not a pretty solution but it certainly made a very tedious job a lot less tedious, and the code that resulted was probably a lot more consistent-looking than if I had spent a week or two writing it all by hand.

SO list of examples of metaprogramming: https://stackoverflow.com/questions/237425/what-are-the-coolest-examples-of-metaprogramming-that-youve-seen-in-c

Solution 5 - Language Agnostic

I can give my own specific example: I am developing ABSE, which is a meta-programming approach. With ABSE you create a model (actually, a tree) where each item is an "Atom". This Atom represents a "concept" and contains the necessary meta-data for its definition.

In ABSE, the implementation of a concept is actually a "mini-program".

Then, the host modeler (AtomWeaver, developed alongside ABSE) takes the model and "weaves" a generator program out of all its Atoms. That program is then run, generating the desired artifacts (source code, data, etc).

So, the ABSE workflow is:

  1. Create a discrete concept (a fraction of the meta-metaprogram)
  2. Reuse that concept in a model (effectively building the metaprogram)
  3. Host modeler weaves and runs the metaprogram
  4. The metaprogram generates your final program

At first sight this looks like a lot of redundant, complex work, but it is actually quite straightforward if you grasp the concept.

Advantages of meta-programming (not exclusive to ABSE)?:

  • Changing the model and regenerating a complete system (Imagine refactoring features instead of source lines).
  • Changing a few definitions in the model can result in distinct programs (a Software Product Family).
  • By reusing templates, you can change the template's code, regenerate and get your code changed in dozens, hundreds of places.
  • Many others, really

Metaprogramming, code generation, program transformation are new exciting worlds in software development, IMHO. However, metaprogramming requires a new skill: meta-thinking.

We can define meta-thinking as "thinking about how you think about your own development". A kind of class reflection, applied on yourself. In practice, you must find out your own development patterns, isolate them, make them generic, and then turn them into metaprograms using your favorite technique, being it ABSE, DSL's, DSM, etc.

Solution 6 - Language Agnostic

Metaprogramming based libraries/code help write directly explicit and simple code that will generate implementation details code for you, depending on parameters used.

Boost is full of (C++) libraries that demonstrate what can be achieved with metaprogramming. Some good (and maybe hard to understand) examples are Proto that allow implementation of DSL, Spirit that allow to write a compiler using EBNF grammar directly inside the code, and many other blow-minding libraries.

Solution 7 - Language Agnostic

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 .

Solution 8 - Language Agnostic

A specific example of where it could be a useful approach.

You have a set of third-party classes, to which you want to add generic behaviour - for example some kind of security/access control, mapping out objects as JSON, etc.

You could write or generate sub-classes for everything, adding wrapper methods to add in access control and call the superclass. With meta-programming, you can do that at runtime, and also your changes will be automatically applied to any additional / changed third party classes.

With the JSON example, by using introspection of the class you should be able to generate the code to serialise an object, and then add this as a method to the class. The other extremes would be generating or writing the code upfront (before compilation) and impacting every time the class changes, or a completely generic approach that used introspection on each individual object, each time you wanted to map it.

Depending on the language and runtime in question, a metaprogamming approach is likely to be faster than the wholly generic/introspective one, but slower that upfront code, as you have reduced a lot of data lookups into code.

Where meta-programming doesn't exist directly in a language, it also seems to me that it is often re-invented through frameworks (i.e. IoC style containers like Spring).

Solution 9 - Language Agnostic

Start your Visual Studio (Eclipse, Netbeans, whatever else). Create a new project. Surprise - you've just used some metaprogramming, by creating a project from a template. Isn't it practical?

Solution 10 - Language Agnostic

You could look at Common Lisp's macros or C++'s templates and see how they're used. Both are metaprogramming in the sense you're using. You'll find that both are used heavily in a lot of code.

Lisp macros are often used to redefine the language. As an example, the last chapter of Paul Graham's On Lisp creates an working object-oriented extension for Common Lisp. Another example is the now-defunct Garnet.

The old Standard Template Library for C++ (mostly incorporated in the standard library) was a way of introducing a large number of containers and algorithms that worked as if they were built into the language, at least in terms of integration and efficiency (not syntactically).

Solution 11 - Language Agnostic

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWayne WernerView Question on Stackoverflow
Solution 1 - Language AgnosticBenoîtView Answer on Stackoverflow
Solution 2 - Language AgnosticIra BaxterView Answer on Stackoverflow
Solution 3 - Language AgnosticGeorg FritzscheView Answer on Stackoverflow
Solution 4 - Language AgnosticFrustratedWithFormsDesignerView Answer on Stackoverflow
Solution 5 - Language AgnosticRui CuradoView Answer on Stackoverflow
Solution 6 - Language AgnosticKlaimView Answer on Stackoverflow
Solution 7 - Language AgnosticJeffrey SchaffnerView Answer on Stackoverflow
Solution 8 - Language AgnosticJulesLtView Answer on Stackoverflow
Solution 9 - Language AgnosticSK-logicView Answer on Stackoverflow
Solution 10 - Language AgnosticDavid ThornleyView Answer on Stackoverflow
Solution 11 - Language AgnosticColm BhandalView Answer on Stackoverflow