Whilst I'm on the subject of code communication

I’ve never really got to grips with the STL’s algorithms; actually that’s not strictly true, it’s really just for_each() that I have most problem with. To me, code that uses for_each() simply doesn’t communicate as clearly as code that uses an old fashioned loop.

What brought this to mind originally was Ben’s post on Code Circle about custom binders for member functions

This code just doesn’t do it for me:


void System::resetAllPasswords() 
{
   for_each( users.begin(), users.end(), 
      boost::bind(&System::resetAndStorePasswd), boost::ref(*this), _1 ));
}

Sure it’s clever, but is it really better than this:


void System::resetAllPasswords() 
{
   for(UserVector::iterator it = users.begin(), it != users.end(), ++it)
   {
      resetAndStorePassword(*it);
   }
}

He explains why he ends up needing to depend on boost for his binder and the version that uses boost is certainly better than the alternatives without, but even so, IMHO, there’s still noise there and that noise gets in the way of the code’s communication…

Note that I don’t have a problem with the more complex algorithms but equally I don’t find myself using them that often. Perhaps it’s just another corner of C++ that I need to spend a little more time with.