Although I often find generators useful, code generation, at least the template sort, is little more than automated cut and paste. I don't consider either a very flexible mechanism at all.
- modifications are not unrestrained. A template driven code generator can only substitute text where the template says it can. To modify the generated code beyond what is allowed by the templates, you have to revise the generator.
- generated code can become a maintenance nightmare unless your generator is kind enough to tag its generated code with comments indicating versions of both the data source and the generator software. Otherwise, you have nothing to go on but date comparisons. However, date comparisons are only helpful if you have customized your build process to be aware of all your generators, often a lot of work. And even a customized build system won't help if you have no way of comparing the version of the current generator to the version that produced the generated code. Code generated by old and new versions of a generator aren't always compatible with each other or the current project requirements. But note: These issues largely disappear if code is always generated from data/source files as described by Tanktulus below. In that case, it is no riskier than generating .o files from .c files.
- it can become even more of a maintenance problem if the generation process is incomplete and the generated code is hand modified. Unless the generator makes advanced provision for hand modifications, the generated code can't be regenerated. If the original generation process spewed out buggy code, you may be faced with hundreds or even thousands of lines of generated code that has to be found and hand modified.
The most flexible mechanism is well designed code that uses the right mix of programming paradigms (OO,procedural, functional, etc). Getting the right encapsulations and abstractions isn't always easy, but when it works, it really works and can grow and change quickly with your requirements. Solid design is more flexible because:
- it avoids replication of bugs - all code is located in one place rather than copied all over.
- it is easily composible
- if bugs are found, the code can be fixed internally without needing to revise consumer code.
- manual modifications can be made via subclassing or custom hooks, making it possible to regenerate even after
extensive manual customization
- support for polymorphism (via objects, dispatch tables, or switch statements) can be built in so that the code changes behavior based on its environment (operating system, server loads, end user preferences, etc)
So, I would actually put your list in reverse order:
- Well designed libraries - capable of real time adaptation and localized modifications that do not affect manual customizations and consumer code.
- Specific language constructs, e.g. switch statements, callbacks, inheritance - allows adaptive behavior for a narrow range of functionality
- Code generation and cut and paste - one off static modifications.
Best, beth
Update: revised comments on maintenence+build process to incorporate Tanktulus's excellent description of "safe" code generation practices.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.