Note: I'm tagging this Python and C++ because I've seen examples in both, but the question is language-agnostic.
A function or class method that modifies an object has two choices: modify the data directly in the object in question, or create a new copy and return it while leaving the original untouched. Generally you can tell which is which by looking at what's returned from the function.
Occasionally you will find a function that tries to do both, modify the original object and then return a copy or reference to that object. Is there ever a case where this provides any advantage over doing only one or the other?
I've seen the example of the Fluent Interface or Method Chaining that relies on returning a reference to the object, but that seems like a special case that should be obvious in context.
My first bad example comes straight from the Python documentation and illustrates the problem of mutable default parameters. To me this example is unrealistic: if the function modifies its parameter then it doesn't make sense to have a default, and if it returns a copy then the copy should be made before any modifications take place. The problem only exists because it tries to do both.
def f(a, L=[]):
L.append(a)
return L
The second example comes from Microsoft C++ in the CStringT::MakeUpper
function. The documentation says this about the return value:
Returns a copy of the string but in all uppercase characters.
This leads one to expect that the original remains unchanged. Part of the problem is that the documentation is misleading, if you look at the prototype you find that it's returning a reference to the string. You don't notice this unless you look closely, and assigning the result to a new string compiles with no error. The surprise comes later.