Test the zero case first

When starting a new "unit of test" (method, interface, module, ...), the first test to write should define its behaviour in the "zero case" i.e. when zero/null/empty goes in, or out.

First, the zero case is often the easiest to implement. Just return "zero", throw an exception or something like that. And because the feature itself is easy, it is effective at driving the creation of the new unit, satisfying dependencies in the test environment and otherwise getting started.

Second, the zero case is often a nasty boundary case that gets overlooked without the test. Sometimes (often with collections) it is automatically OK, but too often it is not.

Originally published on 2009-08-27 at http://www.jroller.com/wipu/entry/test_the_zero_case_first under category Art of programming