Lessons learned trusting Code Generators

To give you a short answer beforehand: Do not do it! But let me tell you my experience:

I started using Jersey back in 2007 when it was at version 0.4. Netbeans however already had a plugin to get you started on the fast track by generating REST Webservices out of Entity Classes, which you can also generate out of an existing db schema. At that time I was only starting learning JPA and was happy that the Netbeans Code Generators took care of most of my Entity Access Code. In retrospective I should have made the effort of learning everything JPA beforehand and not gradually as I worked along. Usually I actually would have done that, but the Code Generators tricked me into not doing it: It just seemed too simple to bother.

Here is what Netbeans generated to get an object by primary key:

Foo result = (Foo )service.createQuery("Select e from Foo e where e.id = :id").setParameter("id", id).getSingleResult();

Compared to:

Foo result = (Foo )service.find(Foo.class, id);

Problem with first approach besides being the obvious bloathed version: this will likely not be an easy cache hit wheras the second method, depending on your persistence provider will likely go a direct way to find the object within an identity map in the second level cache.

No big deal you might say just change the Query to a Finder method. Unfortunately its not just done with this because the getSingleResult also throws an unchecked NotFoundException when the Entity was not found, opposed to returning null in the finder method. In the generated code this exception was catched, wrapped and rethrown as a checked one. Nice so in some parts of my code was building logic upon those rethrown Exceptions when an entity was not found. So changing the way to retrieve an object had a long tail unfortunately.

Besides that the bigger problem is the design of an efficient and effective object domain model. I am speaking about fetch definitions, caching strategies, lazy / eager loading, and all that. At that time I was frankly just overloaded with a lot of other work beside the JPA stuff so I basically thought that the defaults that Netbeans generated might just work out well. Well no no no.

Another example are the Web Services Generators in Netbeans 6.7. As it turned out, the plugin developers have sometimes simplified-out methods as with the Facebook Service: There is no batching. You probably won’t write a Facebook application without using the batching support.

It feels to me that nowadays many IDE’s and tools come out with the message that they will do all the work for you. What you have to do is just some simple plumbing: enter an API key here, connect to your datebase there and you are done. My experience however is, even for a quick learning curve (and not learning by a bad example), it is better not to trust those generators in the first place and only use them if you can easily spot wrong code and correct such pieces yourself.

This entry was posted in java and tagged , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

One Comment

  1. اليهود نتن
    Posted October 31, 2009 at 12:11 am | Permalink

    /me is goin the same path with an eclipse based generator.. learning how to use the wizard and all the dialogs for descriptors is just DADA..