Tuesday, March 16, 2010

Code generation and MDA

"Wouldn't it be great if dummy code could be generated ? "

Yes, a great idea that turns out not to be a great one at the end.

I remember the code generation by the Rational Rose IDE. I used it back in early 2000's creating a lot of class, collaboration and sequence UML  diagrams We even made some specific VBA to extrapolate state diagrams, and then generated nice classes, that were basically skeletons. So you have nice diagrams with  the code matching it, and the "Architect" is happy with all this artwork.

Then refactoring came in the picture, the upfront diagrams did not hold very well of course, and at the end those nice diagrams were out of sync with current code, making them quite useless, (but the "Architect" can still show off  his MDA approach of the project to some gullible people).

Another example : generating POJO classes from the database schema/
You can use an hibernate tool to generate, from the database, the associate mapping and the corresponding POJO.
First that remove the ORM from the "hibernate is an ORM persistence framework".
But besides that the very annoying part is that you cannot modify the generated POJOs. Let's say that you want to modify a field, or rename it. Since you would need to modify the create a DLL script, modify the dB, generate the classes, and refresh it in your IDE, more likely you will never evolve to do simple refactoring. You can't do it directly in your IDE. That case goes actually a bit beyond simple code generation and MDA, since it is also about database evolution. Great article and author book about it.

But back to the subject, sometimes the generated classes are missing some behavior that you want to add ( maybe a specific logic for that object - remember that you are still doing OO, or a well defined equals method often missing)... can't do it.

Conclusion:

You must not modify generated code, but you want to since it is part of your compilation / IDE.

So now, why someone would want to generate code?
Because the code is dummy and is redundant with DataBase/UML Schemas/ Existing Code

2 ways I see it :
 - those dummy classes should not exists or should act behind the scene. If the code is generated means there is no logic inside so why even the need of the code ? Use dynamic proxies, dymaic language, anything so that it is not part of your compilation environment / IDE..
 - the code is the design. Diagrams could be used for a sketch but the current design is the code.

No comments: