More on testable code 25 Jan, 2012
Following on from my previous post, I stumbled across a most excellent resource, Miško Hevery’s Guide: Writing Testable Code. This is detailed, helpful and above all eminently readable—well worth running off and keeping.
Hevery’s document contains a number of tips designed to make code more testable, but really it goes wider than that. Hevery picks out four basic flaws, gives examples of each, how to detect them (“warning signs” if you will), and of course, how to address them.
The four flaws:
- “Constructor does real work” (don’t do any more than basic assignment, and certainly don’t do any static “initialisation” method calls)
- “Digging into collaborators” (avoid a deep chain of wrapper objects)
- “Brittle global state & singletons” (globally-accessible elements can / will cause havoc when trying to test and debug)
- “Class does too much” (remember the classic principle of “single responsibility.” Also, spaghetti code is blummen’ hard to test)
Read more… Guide: Writing Testable Code.