Metaprogramming: Writing Programs that Write Programs

Programs typically read input data, operate on that data and give some output data. Metaprograms read another program, manipulate that program and return a modified program. Sometimes a metaprogram can change its own behaviour by updating itself. The act of writing metaprograms is called metaprogramming. The language used to write metaprograms is called metalanguage.

In general, where code itself needs to change with the data, metaprogramming can be an effective approach. Metaprograms treat code as data, and data as code.


While metaprogramming has been around for decades, it only from the mid-1990s that it received increasing attention. More and more languages are supporting metaprogramming. Principles of metaprogramming have led to higher levels of abstractions such as meta-modeling, metadesign, metaengineering and Model-Driven Engineering (MDE).

What's the formal definition of metaprogramming?

One definition sees metaprogramming as introducing a higher level of abstraction: the technique of specifying generic software source templates from which classes of software components, or parts thereof, can be automatically instantiated to produce new software components.

We could define metalanguage thus: any language or symbolic system used to discuss, describe, or analyze another language or symbolic system is a metalanguage. This leads to the definition of metaprogramming: creating application programs by writing programs that produce programs.

Another definition that emphasizes program generation: metaprograms manipulate object-programs where metaprograms may construct object-programs, combine object-program fragments into larger object-programs, observe the structure and other properties of object-programs. A simpler definition is, Metaprogramming is writing programs that themselves write code.

By ... Devopedia