Functional Refactoring in C++
Consider the following C++ code snippet:
bool UNIVERSE::Initialize(const std::string & assetpath,
const std::string & shippath,
const std::string & behaviorpath,
MODELMANAGER & modelmanager,
std::ostream & info_output, std::ostream & error_output)
{
if (LoadAssets(assetpath,
modelmanager,
error_output))
{
info_output << "Loaded universe assets successfully" << std::endl;
}
else
{
error_output << "Error loading universe assets" << std::endl;
return false;
}
if (behaviors.Load(behaviorpath,
error_output))
{
info_output << "Loaded AI behaviors successfully" << std::endl;
}
else
{
error_output << "Error loading AI behaviors" << std::endl;
return false;
}
... etc ...
return true;
}
We have a function that performs some initialization and returns true on success or false if it fails. Internally the function calls various loading functions. There's some obvious code duplication here; for each load function we call we do almost exactly the same thing to handle success or failure. In the future we might want to change what we do on success or failure, and it'd be nice to only have to change the code in one place instead of after every load call. How can we remove this duplication?
We could make OnLoadSuccess and OnLoadFailure functions and use them like so:
if (behaviors.Load(behaviorpath,
error_output))
{
OnLoadSuccess(info_output, "AI behaviors");
}
else
{
OnLoadFailure(error_output, "AI behaviors");
return false;
}
This is better, but there's still some duplication and it's not as generic as it could be; for example, if we wanted to display a "retry" message box to the user and allow them to retry a particular load step, we couldn't do that without changing all of the places where we called OnLoad*. We can go further.
We could make some variadic LOADFUNCTION macro, but that could get pretty ugly.
We also could refactor all of our loading functions to conform to some standard interface so we could call them in a loop. That's not a bad way to go, but it turns out that we can get similar genericity without having to do that.
In functional programming, it's very common to pass around functions, and it's also easy to do partial function application (currying), which means taking a function object with multiple arguments and supplying some of those arguments, yielding a new function (called a closure) that takes only the arguments you didn't supply. We can do something similar with the TR1 functional extensions. The std::tr1::function class lets us use function objects, and the std::tr1::bind function lets us create something like a closure. We can bind all of the necessary arguments to our load functions and then pass them to a GenericLoad function as a zero-argument function object that returns a boolean. Here's what this looks like:
bool GenericLoad(std::ostream & info_output,
std::ostream & error_output,
const std::string & name,
std::tr1::function f)
{
if (f())
{
info_output << "Loaded " << name << std::endl;
return true;
}
else
{
error_output << "Error loading " << name << std::endl;
return false;
}
}
bool UNIVERSE::Initialize(const std::string & assetpath,
const std::string & shippath,
const std::string & behaviorpath,
MODELMANAGER & modelmanager,
std::ostream & info_output, std::ostream & error_output)
{
if (!GenericLoad(info_output, error_output, "universe assets",
std::tr1::bind(&UNIVERSE::LoadAssets, this,
assetpath,
modelmanager,
std::tr1::reference_wrapper(error_output))))
return false;
if (!GenericLoad(info_output, error_output, "AI behaviors",
std::tr1::bind(&behaviortree::TREES::Load, &behaviors,
behaviorpath,
std::tr1::reference_wrapper(error_output))))
return false;
... etc ...
return true;
}
Is this a win? We've removed a bunch of redundant code and made our loading much more generic. The syntax, though, is absolutely horrible. In languages with first-class functions and a lot of syntactic sugar (like Haskell), the syntax is often neater for the case where we use the generic loader. In C++, an argument could be made that the generic loader is actually harder to maintain in some cases due solely to how confusing and muddled the syntax is.