|
Writing a Code Generator in Java
http://www.ftponline.com/javapro/2004_03/online/code_gnaccarato_03_03_04/default_pf.aspx
In software, one of the most common (and insidious) bugs is the "copy and paste" bug.
You write some code, it looks good, so you paste it somewhere else instead of putting
it in its own method and invoking it in the new place. Later on, you find that your original
code wasn't so great after all, so you fix it in the original place, but not in the place
you pasted it, and BAM you are the proud, new owner of a software bug.
The solution to this type of bug is to store the information (in this case, the source code)
in only one place, and then refer to that place everywhere else. Thus, when the information changes,
the information gets updated but the references stay the same.
(This is also one of the driving ideas behind data normalization in database systems.)
Also, by creating such a reference (in this case, a method name), you facilitate
the reuse of this resource.
So how does this relate to object models? Well, if you can develop a piece of software that
generates all of the source code for your system from an object model, then you can restrict
yourself to working with the model and not the code. Thus, when you make a change in the model,
you can rerun the code generator, and your thousands of lines of source code are automatically
updated. By making a change in one place, potentially hundreds of other updates are
performed reliably and automatically.
As the size of your object model will likely be much smaller than the size of your source
code, this makes it a compact an effective tool to work with.
Obviously creating such a code generator is a difficult (and unsolved) problem,
but read the article to learn more. Even if you are not interested in code generators,
it may be helpful to read this article to start thinking about the useful abstractions
that such a tool could provide. Although the Platonic code generator is not available,
its ideals may be helpful when thinking about software design.
|