Making code test-able 17 Jan, 2012
There are two things a developer needs to make his or her output testable (and therefore more robust. Hopefully):
- The “How would I test for xyz” mind-set, and;
- A fast simple development environment
That’s it, that’s all you need. The first comes with practise, and the second is pretty straightforward nowadays. Of late I have been writing a lot of PHP in Eclipse, Coda and the new kid on the block, Sublime Text 2 (check it out: very nice). All of these tools make it easy to write test-able code, because one simply pulls in the unit testing framework of choice, and then one writes code: job done, very low barrier to entry.
As a small aside, I am constantly amazed at how much PHP stuff goes out the door with minimal-to-no tests, especially when one considers the fluid nature of the language (its typing and such). This contrasts sharply with the mind-set we see amongst Rubyists, who regard their language’s dynamism as raison d’être for excellent test coverage.
So a diligent approach to testing is one thing, but contrast my comments about editors above with other recent experiences writing Java in an Eclipse-based editor called Domino Designer (some of you may be familiar with it). Making that code test-able has been more problematic, given DDE’s reluctance to play nice with plug-ins like JUnit, and the way a typical Java agent is structured. So, a couple of tips:
- Abstraction is key: write as much of your Java code as you can abstracted away from the Domino object model. This way you can code in a proper Eclipse instance, and you can write easy test cases. Break up big problems into small solvable components, and test them. Your Java agents should be very “light”—minimal code perhaps just looping a collection or whatever. Let your custom, tested classes do the heavy lifting.
- If you’re like me, and not at Lotusphere, you will be missing out on a session from Messrs. Myers and Robichaux covering effective Java in the Domino environment. As soon as their presentation is made available, I have it on good authority that you will want it, and that the Wookiee has some tricks up his sleeve when it comes to JUnit :-).
(Finals words of “wisdom”: it is a lot quicker, and simpler, to write test-able code up-front. Adding tests after the fact is always more burdensome).