Monday, October 3, 2011

Unit Testing in PHP

I'm currently working with PHP. I found this bit of code in one of the unit testing tools for PHP. It's not terrible, but it's not great either. To me, it represents one of the tipping points for the value proposition of unit testing in the first place: Having solid unit-test coverage of code should also mean that we can take an extra few minutes to fix it.

public function appendValue($value)
{
   if (is_null($value))
   {
      $this->append('null');
   }
   elseif (is_string($value))
   {
      $this->_toPhpSyntax($value);
   }
   elseif (is_float($value))
   {
      $this->append('<');
      $this->append($value);
      $this->append('F>');
   }
   elseif (is_bool($value))
   {
      $this->append('<');       
      $this->append($value ? 'true' : 'false');
      $this->append('>');
   }
   elseif (is_array($value) || $value instanceof Iterator || $value instanceof IteratorAggregate)
   {
      $this->appendValueList('[', ', ', ']', $value);
   }
   elseif (is_object($value) && !method_exists($value, '__toString'))
   {
      $this->append('<');       
      $this->append(get_class($value));
      $this->append('>');
   }
   else
   {
      $this->append('<');       
      $this->append($value);
      $this->append('>');
   }
   return $this;
}

Tuesday, September 27, 2011

Firsties!

Welcome to Code Exposure, a site for a community that wants to learn to read and understand code more quickly and thoroughly.
In these pages, we hope to provide (within the limits of legal permission) small expanses of code for our friends and collaborators and interested strangers to analyze. We will accept any number of insights from "this sux" to "this code reveals a corporate mindset that is interesting..."  It is not all about making fun of other people's work (we have The Daily WTF for that), but rather for stretching our code reading skills.
All are invited (well, other than blog spammers) to jump in, legally post code snippets, and join the conversation.