PreviousNext…

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.

Comments

  1. good findmark myers#

Comments on this post are now closed.

About

I’m a developer / general IT wrangler, specialising in web apps, the mobile web, enterprise Java and the odd Domino system.

Best described as a simpleton, but kindly. Read more…