More on testable code

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:

  1. “Constructor does real work” (don’t do any more than basic assignment, and certainly don’t do any static “initialisation” method calls)
  2. “Digging into collaborators” (avoid a deep chain of wrapper objects)
  3. “Brittle global state & singletons” (globally-accessible elements can / will cause havoc when trying to test and debug)
  4. “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.


  1. good findmark myers#

Comments on this post are now closed.


I’m a software architect / developer / general IT wrangler specialising in web, mobile web and middleware using things like node.js, Java, C#, PHP, HTML5 and more.

Best described as a simpleton, but kindly. You can read more here.