[GAS] Explains: What is Metaprogramming?

By Sterling “Chip” Camden
Contributing Writer, [GAS]
In recent years, the Greek root meta has perhaps become overused. Originally, it was just a lowly preposition meaning “after”, “beyond”, or simply “with” – but especially since the writings of Douglas Hofstadter it has taken on the meaning of a higher level of abstraction, especially a self-referential abstraction. That’s the sense in which it is used in the term metaprogramming – modifying programs programmatically, or modifying the programming language itself.
As with many terms that describe programming, metaprogramming admits of many different incarnations, shades of meaning, and degrees of support. In the broadest sense, the simple act of creating generalized functions or classes represents an extension and abstraction of the “language” used for programming – but the term “metaprogramming” is usually reserved for more radical modifications. Languages that provide features for those types of operations are often called dynamic languages.
Generative programming
One use of the term “metaprogramming” refers to programs that generate or manipulate their own code. Languages that provide the best support for this are those that easily overcome the distinction between code and data. In more than fifty years since the introduction of Lisp, no other language has devised a more radical yet natural representation of that interchangeability. Code and data are both represented in Lisp as lists, so any list can easily be treated as either code or data. It’s simple, therefore, to manipulate code as data, and then execute it – either via EVAL or by returning it as the result of a macro expansion.
By comparison, COBOL’s single contribution to metaprogramming, the odious ALTER statement, seems laughable. If GOTO is deemed harmful, then ALTER was pure evil. It allowed you to change the destination of a GOTO statement at runtime, producing stealth spaghetti. It had all the pitfalls of metaprogramming and none of the benefits, since it reduced readability without improving abstraction.
...
Read more...