You are here:

Lessons Learned from years Code Generation

EXECUTIVE SUMMARY OF THIS PAGE: Database Table I/O is almost the only use case for generated code. Of course, that covers about 85% of most code written :)


It is interesting that one of my most important mentors who is always highly supportive - Michael Nash - has been almost silent with respect to code generation, excepting one very pointed criticism. When someone I respect as much as Michael criticizes, something he almost never does, I listen.

His criticism of code generation in general is quite simple. Generated code is almost always a dumb idea, and code that can be generated should almost always be simplified and written once as a higher level of abstraction, at the framework level.

This has been an important lesson for me. My first reaction was to simply stop generating code, which I did for a year as I considered Michael's analysis. But then, I did come up with one very pointed and singular use case, and I have pretty much stuck to this use case from that point forward.

Virtually all applications point to database tables and fields, usually lots and lots of them. This code is actually some of the most verbose code, and from this code is also derived table headers and form headings. This is code that can, but rarely works well as framework code, it is almost always customized beyond the pointers in the database.