E. Robert Tisdale said:
There are some exceptions where operations must be performed "in
place" on a variable object but there is seldom any excuse for
returning void.
What a beautiful dogma. Why that? It's just my decision to return
something or not.
You should always return something -- an exception (error code) or a
reference to the the object that was modified if nothing else. This
is a long standing tradition in C and C++.
Tradition is also to use int or char instead of bool.
Consider, for example, strcpy(char*, const char*) which returns a
pointer to the destination string or operator<<(std:
stream&, int)
which returns a reference to the output stream which it modifies.
These return values are for concatenation of multiple following
operations. They are useful.
I have a library of special string functions based on the STL string
class. These functions always return a string object. Nobody asks for
the time needed to create these temporary objects but everyone wonders
"It's not very fast, isn't it?". Nobody thinks about heap
fragmentation. Until now I eliminated more than two millions of
absolutely senseless string objects created in just one single minute
of runtime. Tell me, is this paradigma ("always return something")
useful in this case?
There is a lot of situations where indeed no result is to return,
where no result exists, where it needs just more time to create and to
evaluate the result than to return just nothing. Why the hell should a
sleep-function return something? And what?
Using a bad C++ compiler is *not* a good excuse for writing bad C++
code.
You can really be proud on you for knowing the bad and the good things
in the world. Have you ever been working on a system with a *given*
compiler, a compiler that cannot be changed for several reasons, a
compiler you just have to live with, and a manager who wants still a
good program?
If your C++ compiler does not implement the Named Return Value
Optimization (NRVO), you should be shopping for a new C++ compiler
that *does* implement the NRVO.
As long as I know how the compiler works and what I have to do to
write a quality program, a readable one, a fast one, a program that
runs on *any* compiler, I will do this. This is my profession. And my
profession is also to know these things rather than to assume every
compiler will surely do it right and fast.
I think it's not always a bad idea to change function parameters
especially if there are more than one. I use it seldom but sometimes
everything else would be just more complicated. I always mention it in
a comment. I name the function in that way
bool GetLoginValues (string& uid,string& pwd,string& host);
And I strictly use "const" for everything that *is* const. There's
nothing evil.
T.M.