Best Practices for Strings and Regular Expressions
The word that matters most is ownership. Before the return, the function owns the string. After the return, the caller owns the same string object. The question is not only what the caller can read, but who is responsible for the object now.
Building a String Versus Handing It Back
Functions often build a string and then give it back to the code that called them. These are two separate steps. Building the string is one step. Getting that string back to the caller is another, and the second step is where efficiency becomes a question. This article focuses on one specific pattern, returning a string by value, and on the idea that makes this pattern efficient: relying on move semantics.
What do you think happens?
When a function has built a string and is about to return it, who owns the string object: the function or the caller?
Reveal answer
The function owns the string object before the return.
The source states that before the return, the function owns the string because the function created it. The rest of this article follows what changes when the return happens.
Keep one question in mind as you read: when a function hands a string back, what happens to the string object itself? Answering that question step by step is the goal of the sections that follow.
Tracing Ownership Before the Return
Start with the moment just before the function returns. The function created the string, so the string object belongs to the function. The caller has not received it yet. The diagram below shows the three parties in this exchange and the order of the steps they take.
Read the sequence from top to bottom. The function builds the string first. The return then sends the string out. The last step is the handover, in which responsibility for the same string object moves to the caller. The string object itself is the thing being passed along, and the sequence tracks who is responsible for it at each point.
What Returning by Value Means
Returning a string by value means the function sends the string itself back to its caller as the result.
The phrase by value describes the form of the result. The function is not handing back a label that points to some other place where the string lives. It is returning the string as the value that comes out of the function. In the reasoning in this article, the name s stands for the string the function built, and returning it by value is the act of sending that string out.
The word that matters most is ownership. Before the return, the function owns the string. After the return, the caller owns the same string object. The question is not only what the caller can read, but who is responsible for the object now.
Move Semantics as an Efficient Handover
The source identifies relying on move semantics as the approach used when returning strings by value. Move semantics describes an efficient transfer of ownership of an object rather than treating the return as a duplicate. The efficiency comes from the change of responsibility: the object is handed over, not reproduced.
The comparison below sets the two ideas side by side. A copy produces a second object, leaving the original in place. A move transfers the one object to a new owner. Notice that the move column does not describe a second string coming into existence.
Before and After the Return
The diagram below is a before and after snapshot. Before the return, the string is held by the function. After the return, the string is held by the caller. The handover is the step that move semantics makes efficient. The two highlighted places that change are the return value and the caller's variable.
Tracing ownership through one return
A function builds the string 'hello world' and returns it by value. Trace who owns the string object at three moments: while the function is building it, just before the return, and after the return completes.
- 1While building
The function created the string, so the string object belongs to the function.
- 2Just before the return
The string object still belongs to the function. The return has not yet handed it over.
- 3After the return completes
The same string object now belongs to the caller. The source describes this change as the handover that move semantics makes efficient, rather than a duplicate being created.
The string object is the same object throughout. Only its owner changes, from the function to the caller.
Misunderstandings About the Cost of Returning Strings
Learners often reason about the cost of a return using the wrong picture. The points below correct the most common errors, each tied back to the ownership idea.
Common mistake Assuming every return of a string creates a duplicate of the whole string.
Why it failsThe source states that move semantics describes an efficient transfer of ownership rather than treating the return as a duplicate.
Do this insteadAsk whether ownership is being handed over. In the pattern this article describes, the caller takes responsibility for the same string object.
Common mistake Thinking the function still owns the string after it returns it.
Why it failsThe source states that before the return the function owns the string and after the return the caller owns it.
Do this insteadAfter the return completes, track the string under the caller's ownership.
Common mistake Thinking that returning by value means returning a reference to a string stored somewhere else.
Why it failsThe phrase by value describes the form of the result. The function is returning the string itself as the value that comes out of the function.
Do this insteadTreat the returned string as the object being sent out, not as a pointer to another location.
Common mistake Confusing building a string with handing it back. These are two separate steps, and efficiency questions belong to the second one.
Tracing a Returned String
A function builds a string called s and returns it by value to the code that called it. Describe, in your own words, who owns the string object just before the return and who owns it once the return has completed. Then state whether the source describes the return as a duplicate or as a handover.
Hints
- Start with the moment the function creates the string.
- The source says the function owns the string before the return.
- Look for the phrase that describes an efficient transfer of ownership.
Key Takeaways
- Returning a string by value means the function sends the string itself back to its caller as the result.
- Before the return, the function owns the string object; after the return, the caller owns it.
- Move semantics is the approach relied upon when returning strings by value, describing an efficient transfer of ownership rather than a duplicate.
- Building a string and handing it back are separate steps, and the efficiency question belongs to the handover.