Thursday, August 16, 2007

Writing code which writes code

Lately I have had some experience with writing code for some code generators or I could call it “doing Metaprogramming”. This was a new world for me and I must say that in the beginning I was pretty skeptical, but after some time I can really see significant benefits for the developer.

Very very often you have some code derived from some sort of metadata. This could be a database schema, xsd or a third thing. In my opinion you save tons of time writing a tool (if not already written) that generates code based on that metadata. Your overall maintenance burden will be lower and you have the freedom to change the metadata and just regenerate the code again. You will be better prepared for changes.

Frameworks like Hibernate do have tools for doing tasks like this. For example the “POJO Java code exporter” Ant task. If you for example rely on some home made persistence layer then the code generator properly also has be home made.

Having the code generator done all you have to do is “design” your next domain object. During the build you will get your domain class properly also a DAO class and maybe other application specific classes. The code is generated without you typing any line of code and the code will have the style like all the other domain classes generated.

Potentially errors in the DAO layer is fixed in one place – the code generator. This can give you the effect that either do everything work or else nothing is working :-)

Frameworks like Ruby On Rails also have built in code generation and I mentioned Hibernate above. If you have based your application on frameworks like this much of the code generator stuff is already given to you, but maybe it is not enough to suite your application?

Writing a code generator will initially take some time but depending on the application the development time should be compensated. You need to make the judgment – is it worth the effort?

Code generation is not for persistence layer only. A UI generator can also be worth the effort, but maybe a bit more complex depending on the demands for the different screen displays.

I would like to give a little example. Yesterday I needed to add some functionality to an application which uses code generators. The functionality involved the presentation layer and on any given screen display the possibility for adding a specific button (with functionality irrelevant for the example) should be available.

Adding this specific button to the screen display required a HTML form and some Javascript. With no code generators I as a developer would be forced to add this HTML form and the Javascript to every (in this case) JSP page where the button should be present.

I could optimize this a little with having the pain of writing a taglib, but again the taglib must be added to the page. With an extension to the code generator the required code is added when needed - end of story. The code is maintained in one place and no need for a developer to add the specific button functionality to a screen display. A person (maybe the customer) which understands the UI metadata can add the button to any screen display.

I am not saying that the first thing I will do on my next project is to start writing a code generator, but I will for sure try to spot areas where a code generator will be handy and save some development time on repetitive tasks...