News for hackers. Stuff that matters.
Re:The death of if/else cascades, a lightweight alternative by pushingbits on Sunday December 06, 2009 @11:10AM (#979695)
I use this all the time in C++.

  std::map<std::string, boost::function<void (int)>> handlerMap;

  boost::assign::insert(handlerMap) 
  ("football", &handleFootball)
  ("comedy", &handleComedy)
  ("crime", &handleCrime);

  handlerMap["football"](channelNumber);
Makes the code so much easier to understand. And when the handler functions start getting too complex, you can just add another level:

  handlerMap["comedy"]["situational_comedy"](channelNumber);

There are no comments on this comment yet.