« ScaleFreeModel | Main | How to make a Martini »

Introduction to Java code generation

Jack Herrington has a good introductory article on Java code generation on TSS.

On tools for code generation:
There are some tools you should consider when you are architecting the generator:
  • XSLT [...]
  • XML [...]
  • Jython [...]
  • JSP [...]
  • JavaDoc [...]
On the downsides:
The primary problem is lack of maintenance, and this is the one most people have experienced. You often see the remnants of once-generated code in the source code control system which are now tweaked by hand. The solution for this is to integrate the generator as part of the build process and to never check in the generated code. This keeps the generator in the development lifecycle and ensures its maintenance.
This is important advice, but I don't think Jack's expressed it clearly. I've made the mistake in the past of introducing XDoclet generated EJB code into source control. The mistake isn't obvious until you need to rerun the generator - if the generated code has been modified in the interim you'll be forced to merge changes. And we've all run across javadoc that was committed to source control ("for covenience") which wasn't kept in sync with the code base. The process is far easier to manage if the generation is part of the build process as Jack points out - specifically you want to avoid being tied into requiring an IDE or visual modelling tool to simply compile your code, in favour of being to rebuild automatically (the last time I looked, this style of lock-in seemed to be a near universal problem with visual modelling tools). If the generator can't be scripted over or requires manual intervention (read: mouse clicks), that's not good enough.

On introducing code generators:
You need to think about the generator as another engineer on the team who owns and maintains sections of the code in an active manner.
Understandably, this might make some developers nervous. All I can say is that code generation, used well, systematically boosts productivity by letting you focus on the problem at hand.

The full article is here. And if you haven't read it or are looking for more detail, Jack's book, Code Generation In Action is highly recommended.

January 11, 2004 10:43 PM


Jack Herrington
(January 12, 2004 04:09 PM #)

Thanks for the positive feedback. I'll try to refine the message on source control in articles I may publish later.

Trackback Pings

TrackBack URL for this entry: