I hope that all international readers of this blog are familiar with a spork – that handy tool which combines the brilliance of a fork with the generosity of a spoon, all the while saving manufacturing and stocking costs for fast food restaurants around the world.
In my work on the new book The Developer’s Guide to the Force.com Platform (available through this page), I have had to actually soil my typing fingers with the creation of test methods and classes. Although I absolutely believe that testing is a Good Thing, I grumbled just like many of you probably do when faced with the requirement to test at least 75% of my code, and the very desirable goal of achieving 100% coverage.
Through this work, I received some illumination about just how big a difference testing can make to your applications. Testing methods act like a fork, spearing all the various use cases which require verification for logical consistency. But the spoon aspect of creating and implementing testing methods was even more valuable.
There were several cases where I found that I could not properly create test methods to test certain areas of my code. In every one of these cases, I found that refactoring my code not only allowed me to create test methods that exercised the target code, but that actually improved the architecture of my code. Through the discipline of testing, I was able to discover less-than-optimal design patterns in my code.
This second benefit creates even more powerful and long-lasting effects than the first benefit of insuring logical consistency. Better designed code is more efficient and easier to maintain, and the refactoring exercise taught me techniques to use in my future coding efforts.
Testing is a little bit like going to the gym – you may not want to at any given time, but the long term effects more than make up for the temporary reluctance.