xxxInPlace or xxxCopy?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 19 19:33:47 PST 2011


On 1/19/11 9:11 PM, Jonathan M Davis wrote:
> On Wednesday 19 January 2011 18:36:55 so wrote:
>>> And honestly, from the standpoint of code simplicity and
>>> understandability,
>>> there's a lot to be said for making copies being the default rather than
>>> mutation. You can then use the InPlace versions if you need the boost in
>>> efficiency.
>>>
>>> - Jonathan M Davis
>>
>> Isn't simplicity and understandability favors the in-place style on these
>> type of algorithms?
>> As Jesse Phillips said, it is same as sort.
>
> No. I'd argue that it's clearer to see stuff like
>
> auto newStr = replace(str, "hello", "world");
> auto sorted = sort(newStr);
>
> than to see stuff like
>
> replace(str, "hello", "world");
> sort(newStr);
>
> If you have
>
> replace(str, "hello", "world");
>
> you don't know whether it's changed the value in place or if you're throwing
> away a return value. However, if you have
>
> auto newStr = replace(str, "hello", "world");
> replaceInPlace(newStr, "world", "hello");
>
> it's quite clear that the first one returns a value and the the second one does
> it in place. Whereas if you have
>
> auto newStr = replaceCopy(str, "hello", "world");
> replace(newStr, "world", "hello");
>
> the first one is clear, but the second one is only clear because seeing the first
> one makes it obvious that the second one must be doing something different.

This is a good argument, thanks Jonathan.

Andrei



More information about the Digitalmars-d mailing list