Named Parameters

Named Parameters

21 May 2018    

In quite a few languages, you can annotate function arguments when calling one eg:

requestHandler(url:"http://....", retry:false);

For better or worse, C++ doesn’t have such a feature and instead this is what one ends up with:

RequestHandler("http://...", false /*retry*/, nullptr /*custom_data*/);

While this is good practice, you can often do without the ugly C style comments. Instead prefer to create variables for them as such:

bool const retryFalse = false;
void const* customData  = nullptr;

RequestHandler("http://...", retryFalse, customData);

This should also be applied to magic numbers and calculations made in the same line as the function call:

constexpr auto mirrorHorizontal = -1;
constexpr auto mirrorVertical = 1;

symbol.SetScale(mirrorHorizontal, mirrorVertical);

// similarly
auto const left = (Window.width() / pixel_ratio) * 3.0f;
// ....
object.SetCoords(left, right, top, bottom);

It is always better to use the language syntax to represent comments especially when it doesn’t have any additional (performance) costs associated with it.