By Zed A. Shaw

The Wrong Aesthetic

This is a quick one, because hopefully it will require very little explanation. This is wrong.

The author has converted a simple for loop into a usage of boost::bind, but has also attempted to format the for loop to make it look like the same amount of code. Let's compare if I simply rework the for loop to look normal:

int Dice::total() const {
  int total = 0;
  const_iterator i;
  for(i = dice.begin(); i != dice.end(); ++i) {
        total += (*i)->faceValue();
  }
  return total;
}

Now compare that simple common and easily understood block of code to:

int Dice::total() const {
  return std::accumulate(
      dice.begin(),
      dice.end(),
      0,
      bind(std::plus(), 1, bind(&Die;::faceValue, _2))
  );
}

The second one is so completely weird, and so completely different that it would confuse nearly everyone who reads it except the original author.

However, the most obnoxious thing about this throwback to the "Template Metprogramming Insanity of 2000" in C++ is the simple fact that:

They do the same thing.

This is a classic case of the aesthetic of complexity that plagues programming. Programmers when given two equivalent pieces of code, seem to pick the more complex one over the simpler one. This "Inverse Occam's Razor" is what killed C++ in the first place, what killed Java, and eventually killed Ruby for me.

What happens I think is that programmers see the two, and then the novelty of the complex version makes it seem "clean". The old way of doing it is tired and lame, so it looks dirty and ugly. This attracts the coder to the new way of doing it, even if objectively is it worse in every way.

Just because something is new doesn't mean it's better, and in the above examples the boost::bind implementation is completely the wrong aesthetic.

Because when I put the boost::bind version in I added an error by removing one character. Can you find it?