← the matrixstructure

Capability — structure

Metaprogramming

Code that writes code.

LISP’s macros could take apart the forms it was given and build new ones at compile time — the first general tool for making a language say something it did not say. The C preprocessor extended the language by text substitution; C++ templates did it by instantiating; Ruby’s metaprogramming did it at run time, which is why Rails could look like a DSL.

Every one of them trades readability for reach. The cure is usually the same: keep the generated code small, and keep the generator honest.

The trade-off

  • Metaprogramming hides real work behind a name, so the reader meets the generator instead of the code.
  • Macros can break hygiene, shadowing, and tooling — the compiler’s view of the program stops matching yours.

Where it came from

LISP, 1958 — macros from the same syntax the evaluator uses; C’s preprocessor, 1972; C++ templates, 1985.