Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google C++ Style Guide. Why input-then-output parameter ordering?

Tags:

c++

In Google C++ Style Guide, it said:

When defining a function, parameter order is: inputs, then outputs.

Basically Google suggest function parameter ordering like:

void foo(const Foo& input1, const Foo& input2, Foo* output);

However, my colleague suggested that the output should be put in the first position. because in this way, foo could accept default values and most of the time output would not use a default value. for example:

void foo(Foo* output, const Foo& input1, const Foo& input2 = default);

I think what he said make sense. Or is there something we are missing here from aspects of readability, performance, ...? Why the style guide suggest output should be the last?

like image 320
qqibrow Avatar asked Jan 14 '15 01:01

qqibrow


1 Answers

The reason why this isn't a problem for the Google style guide is because default arguments are disallowed:

https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Default_Arguments

We do not allow default function parameters, except in limited situations as explained below. Simulate them with function overloading instead, if appropriate.

Pros

Often you have a function that uses default values, but occasionally you want to override the defaults. Default parameters allow an easy way to do this without having to define many functions for the rare exceptions. Compared to overloading the function, default arguments have a cleaner syntax, with less boilerplate and a clearer distinction between 'required' and 'optional' arguments.

Cons

Function pointers are confusing in the presence of default arguments, since the function signature often doesn't match the call signature. Adding a default argument to an existing function changes its type, which can cause problems with code taking its address. Adding function overloads avoids these problems. In addition, default parameters may result in bulkier code since they are replicated at every call-site -- as opposed to overloaded functions, where "the default" appears only in the function definition.

Decision

While the cons above are not that onerous, they still outweigh the (small) benefits of default arguments over function overloading. So except as described below, we require all arguments to be explicitly specified.

One specific exception is when the function is a static function (or in an unnamed namespace) in a .cc file. In this case, the cons don't apply since the function's use is so localized.

In addition, default function parameters are allowed in constructors. Most of the cons listed above don't apply to constructors because it's impossible to take their address.

Another specific exception is when default arguments are used to simulate variable-length argument lists.

// Support up to 4 params by using a default empty AlphaNum.
string StrCat(const AlphaNum &a,
              const AlphaNum &b = gEmptyAlphaNum,
              const AlphaNum &c = gEmptyAlphaNum,
              const AlphaNum &d = gEmptyAlphaNum);
like image 76
OmnipotentEntity Avatar answered Oct 06 '22 01:10

OmnipotentEntity