Meta-Programming : Code that writes Code!
Meta-Programming is a programming technique in which computer programs have the ability to treat programs as their data.It means that a program can be designed to read, generate, analyse or transform other programs, and even modify itself while running.Meta Programming is about writing code that writes code.
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.
Lisp, introduced in 1958, was one of the first languages to have such capabilities. The reason being that Lisp uses S-expressions for both its syntax and data structures. Therefore, Lisp code can be fed into Lisp programs as input data structures and be manipulated by them! This is somewhat unique to Lisp languages.
In modern languages, meta-programming is represented in different ways. Templates in C++ are a form of compile time meta programming. Other languages use eval, goto, Automatic programming, and other techniques to offer meta programming capabilities in the run-time. A class of those languages that particularly encourages meta programming is Dynamic languages. Such languages are usually dynamically typed and are very flexible in general. Ruby and Python are good examples of this class.
Meta-programming 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 !