Source Code Templating DSL
Ok, so I repeat myself a lot. I like to look at it as a healthy expression of what Freud calls The Repetition Compulsion. If you don‘t have it, you are an infant. It‘s a disease, but it‘s a beneficial one. The sick form is when people were traumatized and are lead repeatedly to restage whatever it was that traumatized them as a way of exorcising their demons. The healthy form is the idea that everything that we do, we know could have been done better, so next time we come back, we are driven to keep questioning how we could do better (that next time). Pillow fluffers call this ‘passion,‘ Lean calls it Kaizen. Ok, weird aside: Freud stole pretty much everything from Nietzsche, but Nietzsche, if he‘s about one thing only it‘s health, whereas Freud‘s focus is almost always on illness.
So I was writing a code template in eclipse last week for making Java unit tests in Akka. The format of the tests is pretty insane. The Typesafe guys blame Java‘s syntactic limitations, but really, it‘s not that much worse than many such testing frameworks, the Seam Test stuff was not much better for instance. It took me almost no time, now I can type in akka and ask for code completion and get a list of my akka templates and choose akkaTestCase and boom, it will walk me through supplying the name, the actor under test, and it will drop me into the middle of the test body. Minor annoyance: the test assumes you have declared a field in the test for the ActorSystem.
That got me thinking: why on earth is there not a DSL for doing code transformations? I have blogged before about how the templating mechanism in eclipse could use a bunch of improvements. You can stipulate imports, which is awesome, but you can‘t designate fields. How hard would that be? Then the parser if it found a field was already there, would ignore otherwise it would drop it into the class.
The other approach would be to make a template for the test class itself, then have one for the methods. That‘s probably the cleanest approach.
Why is there no option to create a class from a template in eclipse??
I hope Apple thinks about doing some of this stuff with snippets. One of the problems in the Java world is that there are two IDEs and so you are not going to get any one way to do any of this stuff.
The other countervailing consideration here is that when you find yourself with a lot of complicated boilerplate, it screams out for some kind of refactoring. I don‘t like the idea of having 20 or 30 tests that all have a construct with 10+ lines of repeated boilerplate. Kind of undoes the whole BDD-aspect of having the tests contain a behavior specification that‘s easy to see.