Day 15: Use Meta Programming When Possible

Meta programming is a style of software development where one is able to modify programs automatically, based on rules that have been previously defined in the system.

For example, suppose that one wants to create a graphical interface based on simple specifications. The UI code may be written as a complicated C program, which makes it difficult to adapt the system every time there are changes.

A better way to solve the problem is to have a separate program that reads a simpler specification for the graphical interface and produces C code that implements the specifications. It may be a little complicated to write a program to read the specifications and write the code in C, but once this is done it is a simple issue of adapting the specification file, instead of rewriting the GUI code.

In meta programming, we are thinking about ways of avoiding writing software by converting simple specifications into real code. By doing this, a programmer can speed up development time, because now the changes can be made by the computer, not by yourself.

The number of bugs can also be drastically reduced, because you need only to check that the generation is of code is correct. Once this is done once, every time there are changes you can rely in the code generator to do the right thing.

Language Support

A few languages make the concept of meta programming easier to use. For example, Lisp provides macros as a standard facility to write code that will be directly read and compiled by the system. Prolog provides simple clause expansions that may be used to generate code as needed.

For more traditional languages, meta programming is not part of the normal development process. This doesn’t mean however that it cannot be used. In fact, more verbose languages such as C/C++ and Java are the best targets for meta programming, because we can avoid writing a huge amount of code with such a facility.

A well known example of code generation with C is the lex/yacc system for generation of parsers and lexical scanners. Instead of doing the laborious work of generating a parser in C or C++, one can use yacc to generate the repetitive parts of the program. Then, one can add only the code necessary to operate on the parsed entities.

Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.

Nowadays it is easier than ever to use meta programming. For example, one can simplify the task of parsing the formal specification by using XML as the specification language. We have standard tools and libraries that can parse XML and generate code as necessary.

The main step in leveraging meta programming is thinking about the needs of the system and take advantage of simpler specifications whenever possible.Using meta programming when possible can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code.

Further Reading

Flex & Bison (Bison is a version of YACC) provide a simple way of meta programming, where code is generated based on the syntax of a particular language.

Generative Programming is a book that describes methods to create systems through code generation. It has many novel ideas that are of interest for practical applications.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

One Response to “Day 15: Use Meta Programming When Possible”

  1. Dear all,
    This is a good attempt full of useful tips.

    Thank you

    By Debo Oladele on Jul 4, 2011

Post a Comment