Well, here we are. Yet another proposed practice that my C++ book has an opinion on. It says “a returning-value(non-void) function should not take reference types as a parameter.” So basically if you were to implement a function like this:
int read_file(int& into){
...
}
and used the integer return value as some sort of error indicator (ignoring the fact that we have exceptions) then that function would be poorly written and it should actually be like
void read_file(int& into, int& error){
}
Now to me, the first one is much clearer and nice to use. If you want to ignore the error value, you do so with ease. But this book suggests the later. Note that this book does not say returning value functions are bad. It rather says that you should either only return a value or you should only use references.
What are your thoughts on this? Is my book full of crap? (again)
The advice is silly. A direct return value is much smaller and easier to type.
Direct return:
Indirect return:
Edit: a bigger issue is whether to use non-
constreference parameters at all. It can be surprising to have side effects come flying out of the parameters. One coding standard says that reference parameters should beconstand output parameters should use pointers. That way the reader gets a&at the point of call that shouts out “something happens to this parameter”.