Metaprogramming (programming)



         


Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at runtime during compile time. This allows programmers to produce a larger amount of code and get more done in the same amount of time as they would take to write all the code manually.

A simple example of a metaprogram is this bash script:

#!/bin/bash # metaprogram echo "#!/bin/bash" > program for ((I=1;I<=992;I++)) do echo "echo $I" >> program done chmod +x program

This script (or program) generates a new 993 line program which prints out the numbers 1-992. This is only an illustration on how to use code to write more code, not the most efficient way to print out a list of numbers. Nonetheless, a good programmer can write and execute this metaprogram in 5-10 minutes, and will have generated exactly 1000 lines of code in that amount of time.

Reflection is a valuable language feature for facilitating metaprogramming.

[Top]

See also

[Top]




  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License