Code Generators

26. August, 2009

Code generators can save a lot of time and effort which leads to the question: Why isn’t everyone using them? A lot of code in any project is repetition.

The main reason: With todays IDEs, you can’t debug generated code. Well, you can debug the result of the code generation but when hunting bugs in generated code, chances are that you want to hunt them in the input and/or the transformer and no IDE has a notion of “this line of code came from these inputs, so show them as well”.

Example: You need POJOs which map database tables. Easy enough: You create a small program that connects to the database, maps the types in the tables to types in your favorite programming language (or rather the one you have to use for your project) and dump some code.

But unlike C/C++, modern languages don’t have a #line directive which says “this piece of code was originally part of another file” let alone “this information came from the database XXX”. So when there is some problem, you need to dig through the layers of the code generator, the templates and the input (the database) yourself, running the transformation in your head. Bad.

Approaches like MDD will keep failing until they support this.