Code Generation

This is a list of some advantages to code generation over fully dynamic programming. It might be useful if you ever need to justify spending time writing code generation tools. After working on a fairly elaborate code generator that created simple SQL table editors for MySQL, I've learned a few things.

  • Generated code has few, or no loops, and that is easier to debug.
  • Generated code allows for fine-grained customization of each part of an application, so that you don't code exceptional situations into the libraries.
  • Generated code tends to self-document the structure of the app a little better.
  • Dynamic code is easier to update... but it's also easier to create more dependencies on the code.
  • Dynamic code is more fun to write.

So why use a generator? Because it unrolls a loop or two, in the right places, leading to faster, less confusing code that, partially, self-documents. Generated code tends to expose the structure of the application, as code. Each chunk of code is specialized, and not responsible for large areas of the application.

Dynamic code tends to be harder to understand, because the behavior of the software can be assessed only at runtime. In a system like PHP, where scripts start up, execute, and exit in a fraction of a second, it's hard to debug dynamic software.