HAYCORN — 17 April 2011

Recommendation: "Writing Testable Code"

A few folks at Google have put to­gether a very read­able PDF book called Writing Testable Code. It covers four flaws, and explains, in ad­mirable detail and great clarity, why they are a bad thing, and how to fix them. I don’t usually like ar­ti­cles like this—I find them too vague and non-specific—but this is a bril­liant exception. The four flaws are:

  • Con­struc­tor does real work
  • Digging into col­lab­o­ra­tors
  • Brittle global state and sin­gle­tons
  • Class does too much

(It’s not just about testable code; it’s about good code in general. Testable code is much easier to reason about than untestable code.)