My TDD addiction

After over two months of deliberately attempting to adhere to the practice of Test-driven development (TDD), I started working on a sizable chunk of untested code. Here are some of my reflections on the contrast between tested and untested code.

TDD is an arduous process. When you’re coding up a solution, not only do you need to figure out what code needs to do, you are also forced to componentize the solution in a way that is unit testable, if only to make the test writing easier. This is especially annoying when you’re working on procedural functionality that doesn’t decompose naturally. This forced decomposition has its benefits, though, as it “encourages” one to produce simpler and more understandable code.

One of the other luxuries afforded by having tests is how easy it is to hand over code from one developer to the next. The test suite provides a very quick and comprehensive way to verify that existing functionality is not broken without the new developer having to be across the board with the codebase. Speaking of verifying functionality, the scaffolding provided by our test suite has allowed us to execute and complete major code refactorings in very little time.

But back to my experience with the untested piece of code. I couldn’t help feeling a sense of trepidation as I worked my way through. Maybe it was because it was unfamiliar territory for me. Perhaps it was because there was no way of me verifying that I hadn’t stuffed anything up without first fully understanding what it does.

But one thing is for sure, the last two odd months have made me very dependent on tests. Addicted even; which makes me wonder if that is a good thing.

gatlingGun shoots holes in your array

Here’s a snippet I created to generate invalid data from a valid key-value array. It’s really handy for testing. Feel free to use it.

   * Gatling gun takes an array of valid params and returns every
   * combination of invalid data caused by blanking out individual params.
   * Example:
   * $validData = array('firstName' => 'Gatling',
   *                    'lastName' => 'Gun',
   *                    'age' => 12);
   * $ggData = gatlingGun($validData);
   * $ggData = array(
   *     array('firstName' => '',        'lastName' => '',    'age' => ''),
   *     array('firstName' => '',        'lastName' => '',    'age' => 12),
   *     array('firstName' => '',        'lastName' => 'Gun', 'age' => ''),
   *     array('firstName' => '',        'lastName' => 'Gun', 'age' => 12),
   *     array('firstName' => 'Gatling', 'lastName' => '',    'age' => ''),
   *     array('firstName' => 'Gatling', 'lastName' => '',    'age' => 12),
   *     array('firstName' => 'Gatling', 'lastName' => 'Gun', 'age' => '')
   * );
   * @param array $validData
   * @return array
  function gatlingGun($validData)
      $original = $validData;
      $invalidCombinations = array();

      $max = count($original);
      $keys = array_keys($original);
      $combinations = pow(2, $max) - 1;

      for ($i = 0; $i < $combinations; $i++) {
           $tmp = array();
           $j = count($keys) - 1;
           foreach ($keys as $k => $key) {
              $tmp[$key] = (($i & pow(2,$j)) == 0) ? '' : $original[$key];

          $invalidCombinations[] = $tmp;
      return $invalidCombinations;

Just be careful, the number of combinations it comes up with is 2^n - 1. So the number of invalid combinations from an array with 10 key-value pairs will quickly balloon to 1023.

Do drop me a line to let me know if this has been helpful for you.