One of the sysadmins at Stuart Caie's company — Jim, as we'll call him — wanted to scratch his itch. Because he spent a whole lot of time logging into different systems just to gather operational data, he figured that a simple web application could automate the data collection process. "This way," he persuadingly said to management, "I can be much more efficient when doing my job."

The brass agreed and allowed him to dedicate some time to building a monitoring application. Jim was excited and ready to overcome his first obstacle: he didn't know anything about programming. Fortunately, Learn Perl in 21 Days and Learn PHP in 21 Days showed him the way and, in no time, Jim had a nice little web app built. His co-workers congratulated him on it and asked him to build in some more features. Management was also pleased with the tool and allowed him to spend some more time in development. And so Jim kept adding to it and adding to it. Again and again.

Fast forward two years later, and the application had become a cesspool of barely functioning copy-n-paste code. With hundreds of data feeds, each freely writing to one central database that was already buckling under strain, Jim spent his entire day firefighting problems with his web application. This, obviously, didn't leave him much time to do his actual sysadmin job and management wasn't all too thrilled about that. Jim aggressively fought off all attempts at transitioning the application to the development team so, during a round of cost cutting, Jim was made redundant.

When the web app was picked up by the development team, it was quickly brought into source control, refactored, monitored, and fully documented. Since it would have been wasteful to rewrite it from scratch, there are still vestiges of Jim in there and, from time to time, the dev team enjoys discovering how Jim thought. On one page, for example, he needed to go through all our servers and count up "install types". Sometimes servers don't have an install type, so that gets counted as "null". It would be easiest to do that as an SQL statement, but in PHP it would look like this:

foreach ($aservers as $x) {
   $type = $ainstall["$x[hostid].$x[hostname]"];
   if (! isset($type)) $type = 'null';
   $avalues[$type]++;
}

Jim, however, didn't write code like that. He wrote this, which does the same thing in a more roundabout way.

 $akeys = array_keys($ainstall);
 for ($i=0; $i < count($aservers); $i++) {
   $hostid    = $aservers[$i][hostid];
   $hostname  = $aservers[$i][hostname];
   $systemkey = $hostid . "." . $hostname;
   for ($j=0; $j < count($akeys); $j++) {
     if ($systemkey == $akeys[$j]) {
       $ainstall_values[$i] = $ainstall[$systemkey];
     }
   }
 }
 $aunique = array_values(array_unique($ainstall_values));

 // PHP has an eval function like perl.
 // Building up a switch statement  to run later.
 $switch  = "switch (\$ainstall[\$systemkey]) {";
 $avalues[count($aunique)];

 // add entry for null values
 $avalues["null"] = 0;
 for ($i=0; $i < count($aunique); $i++) {
   $avalues[$aunique[$i]] = 0;
   $switch .= "  case \"\$aunique[$i]\" : ";
   $switch .= "  \$avalues[\$aunique[$i]]++; ";
   $switch .= "  break; ";
 }
 $switch .= "  case \"\" : ";
 $switch .= "  \$avalues[\"null\"]++; ";
 $switch .= "}";
 for ($i=0; $i < count($aservers); $i++) {
   $hostid    = $aservers[$i][hostid];
   $hostname  = $aservers[$i][hostname];
   $systemkey = $hostid . "." . $hostname;
   eval ($switch);
 }