Tuesday, November 22, 2011

Code that generates code

One satisfying exercise that I carried out today was to automate a seemingly mundane task :-) Using Velocity templates and some reflection in Java I was able to create a bunch of Transfer Objects from Java classes to Objective C source code. No verbose xmls were required but just the jar containing the classes and voila. 234 Objective C classes generated in under 10 secs.

With the prevalence of patterns in programming and seemingly routine stuff that we write one way to make things interesting would be to not write the code itself but write code generators that produces the required code. This would be especially valuable in code bases where multiple modules all have similar code but differ only due to the attributes of the source classes.

Thinking in terms of meta programming would also mean that LOC's of generated code does not matter. If you can do it for 1 class then you can do it for any number of classes.

Some things I see as a potential problem
1) Version history of the generated classes would be meaning less. They would be a by product of code generators version history
2) Exceptions in logic individual modules would lead to lot of branching in code generators.

I feel that the typical service calls, data mapping from layer to layer etc. are excellent candidates for Code generation. This also would be much much more interesting than simply churning out routine code.

No comments: