Steve B was kind enough to include a nice story with his submission. So, here it is, straight from Steve ...

We had a developer working for us years ago who was fond of telling people, "Guys, I'm a genius. Seriously, I'm actually a genius". A year or so after she left, we discovered that our nightly auto-build had become stuck while executing some test code - the process was just burning cpu. We gcored the process and found the routine that had caused the problem - a random number generator written by "The Genius." Thank goodness this routine was not part of our production code.

I think it's fair to say that the following routine is a work of genius - evil genius, maybe. We've all implemented a random number generator at some point in our lives (most of us wrote our first when we were in teenagers), and they usually run one or two lines max. The Genius has not only managed to stretch this logic out to many lines, but has also come at the problem from such a profoundly "unique" direction that hasn't been seen here before. The icing on the cake is that, not only is it profoundly inefficient, but it has several opportunities to crash, contains at least 8 other programming errors, and has the capability to fall into an infinite loop (that's how we found it).

It's like one of those puzzles from "Highlights for Children" - how many things wrong with this picture can you find?

double MUTils::GenerateRandomValue(double theMin, double theMax)
{
  if (theMin >= theMax) {
    // error in parameters, so return 0.0
    return 0.0;
  }

  // seed the random generator if it hasn't already been seeded
  if (0 == itsSeed) {
    this->itsSeed = time(NULL);
    srand((int) this->itsSeed);
  }

  // get a random double
  int anIntegerPortion = rand();
  int aFractionPortion = rand();
  double aValue = (double)anIntegerPortion + 1.0/(double)aFractionPortion;

  // figure out the range the double should fall in
  // i.e. if the min was 0, how big would the max be?
  double aRange = theMax - theMin;

  // make the double be between 0 and aRange
  for (; aRange <= aValue; aValue -= aRange);

  // adjust the value so it's between min and max
  aValue += theMin;

  // all Done
  return aValue;
} // double MUTils::GenerateRandomValue(double theMin, double theMax)

All Done indeed.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!