|
Re: The Problem with Programming
|
Posted: Nov 30, 2006 12:57 PM
|
|
> > For example, one thing I've tried to do lately is > reduce > > the amount of hand-written code in the system. We wrote > > several code generators, and we're planning to add a > few > > more, that generate portions of our system from smaller > > input files in little grammers we devised. So we're > trying > > to replace big swaths of hand-written Java code with > much > > smaller pieces of hand-written code in our little > > languages. It also reduces code because we have fewer > > tests to write, as we don't bother writing tests > against > > the generated code. > > Can I ask why you have so much code? It kind of sounds > like you are automating bad practices. I generate code > sometimes but it's been either to fulfill the requirements > of a foolish design or for things like creating a wrapper > around ResultSet. > I have tended to do code generation on many different projects over the years. The thing that happens is that occasionally I find we are doing repetitive things that we can't eliminate with the abstractions available in the language itself, but we can if we write a code generator. The code generator makes development faster and more accurate. An example is entities. We use Hibernate for database entities, and there is a lot of code involved in that. We also version some entities, so that there's a history and a "deletion" table too. And in that case there's a stored procedure to make the history record. For a versioned entity we needed to write 3 bean-style classes, 3 XML mapping classes, a stored procedure, several persistence methods in a manager class, etc. Over and over again. So we wrote a grammer in which we can capture what it means to be a versioned entity in our system in a few short lines, and generate about 10 .java, pgsql, and XML files from that.
Another example of repetitive code is pulling parameters out of HttpServletRequest in our controllers. We can't put that in a superclass (i.e., use Java's abstraction tools), because each controller has different parameters to grab. So we created a little grammer that allows us to list the params and other information and generate code for each controller. I want to add generation of some code that makes it easier to write unit tests for those controllers, and will be adding one to generate our "view" classes, which are also quite repetitive in a way that can't easily be zapped by OO techniques, etc.
These aren't the kind of code generators you use to get started and then edit the generated code by hand after that. These are the kind whereby the source code of the system moves from the general purpose language to the little language itself, in which we can express what we want in much less code.
|
|