I recently stumbled upon abstratt’s blog post on Anti-patterns in code generation and could not resist the call for comments. My response got a little bit longer so I am posting it here. I may be biased, because I work at Yatta Solutions, but to keep Ted from pulling out his hair forever, I would continue his story something like this:
Finding the ring in the Land of Mordor
Loosing himself in the World Wide Web looking for the right search terms, Ted falls asleep and starts dreaming…
He finds himself in a bare cold land – alone and freezing, only with his Modeling IDE on his laptop. In the far distance he sees his destination: Mount Doom. A shiver runs down his spine. But being a brave developer, Ted starts to find his way through the Land of Mordor. He keeps his distance from all the beasts, struggeling to find his path. His idea: separating model and code! But then, some rather deep pitfalls are becoming evident, more and more single guards (like his team mates) are gathering. The horde is rising! Finally, a good man comes along, a sage. He tells Ted to think of the solution, not the problem. „Ted, you need help to find your way through Mordor. Find your Sam!“
Suddenly, Ted wonders why his modeling tool is no aid at all. It screws up his changes when generating code and jumbles his model when reverse engineering. „What if it was never meant to match model and code?“ For a second Ted’s pale grey laptop seems to have a wicked evil grin.
At the orc camp
Ted takes a rest to think his mission through. An idea is growing in a corner of his mind. He needs help, he needs another tool! Being a good software engineer, he starts to work out his solution. He starts to build yet-another-case-tool! His idea, now: matching code and model as he, the typical enterprise software developer, wants! Ted thinks about pattern and best practices, about all the things his tool will be able to do. He gets really excited about his tool, his precious tool – his one ring…
At the beginning, Ted works out a new kick-ass code generator. In fact, there are many pretty good code generators out there as Ted knows, but he wants his own precious „friend“. So, Ted works hard until it is done. His ring … err, tool … uses templates for code generation. They allow him to stay agile by adapting the code generation to his changing project needs. He develops his own template language – instead of using Xpand/Xtend.
Then, Ted starts working on the reverse engineering: first, he builds a tool to merge all his code changes back into his model. Just having finished (and being a bit jumpy), Ted starts running his tool – and it works! All his code changes were transferred into his model. The model grew and became more detailed. But then, Ted realizes the problem: his model got flooded with implementation details. Getters, setters, toString, iteratorOfStuff … they all got into his pure model – his beautiful clear model, it became ugly and senseless.
After thinking of protected regions – and discarding this non-solution –, Ted starts writing a parser. The parser is meant to recognize code patterns and merge them back into his model. After reverse engineering his project again, Ted gets really excited. „Yes, I did it!“ His model looks as expected, pretty and simple, – and it seems to contain his manual changes, doesn’t it? So, Ted continues developing by modeling some more classes, changing some property names and dragging some new associations. Generating code again, the word „easy“ crosses his mind. Still dreaming, Ted takes a look at the code: „FFFFUUUUUUUUUU… What a nightmare!“ His precious tool screwed up his code again! Somewhere in the guts of the parser there is a mismatch between the details the code generator adds to the code and the details the parser hides from the model.
Suddenly, it hits Teds mind: „Why not useing the same code generation templates for reverse engineering?“
Ted awakes with a start and does a quick google search for „template-based round-trip-engineering“: … „Hmm, a scientific paper… another one … come on, I just had that idea, it ain’t rocket science! Wait, what’s that? UML Lab?“
Ted quickly skimms the section on „Round-Rrip-Engineering NG“. Having a look at the screenshots and videos, he decides to download UML Lab ( from the eclipse marketplace | as a RCP ). The tool tour takes him through a simple example, giving him an idea of template-based reverse engineering, modeling, code generation and hacking. He leans back smiling, finally having reached his goal: truly agile modeling and coding.
His nightmare long forgotten, Ted ( coded | modeled ) happily ever after.