A Cynical Introduction To Testing

Don’t Feel Bad

  • You might feel bad that you write fewer tests than you “should”.
  • “Testing is the dental floss of development” – Ned Batchelder
  • Don’t get mad, get even.

All Code Is Terrible

  • All code is terrible.
  • There is no shame in writing terrible code.
  • There is only shame in having more code than is strictly necessary for a purpose.
  • It’s costly for both the originator and a future reader.

How Do I Avoid Shame?

  • Test everything.
  • It’s an entirely mechanical process. Use no judgment.
  • Accept that it will make you “slower”.
  • Requiring healthy test coverage of your code tends to mean you write less domain code. This is a good thing.

I Mean Everything

  • Wrap your brain around it, roll it around on the tongue: everything.
  • No function too small.
  • Code that is proven to work repeatably is beautiful. Even terrible code.
  • Not unusual to have 2X more test code than “real” code in a given project.

Proving It Works Isn’t The Goal

“It’s silly to prove that a function that does “return 1+1” always returns 2.”

  • Not the only goal. It’s at best a distant third reason.

  • The first and second goals are:

    • detect regressions caused by future maintenance (“return 1+2”).
    • use testing as a thinking tool for improving code in real time.

But It Makes Me Slower

  • Yup. This is on purpose.
  • I’m not a superstar developer. You aren’t either. None of us are.
  • Writing tests for everything might make you go slower “in the small” but the organization as a whole will go faster.

Don’t Be Afraid To Throw It Away

  • I use testing as a way to think about code. I write something terrible quickly, then I start to write some tests for it. The act of writing tests reveals how terrible it is, and I throw some of it out, and write something better. Rinse, lather, repeat until code is good.
  • TDD? Not so much me. If it works for you sure.