Wednesday, May 29, 2013

C++11: copy and decorate idiom

I love to generalize. Some would say "over-generalize". For example if you ask me: "What is Query Language?" I will answer: "A way to create predicates from textual data at runtime". I know, over-generalizations are bad.

Recently I looked at the GOTW#4 solution. To my shame I did not know why and how operator + should be implemented in terms of operator+=. However (once I understood it) I really like the guideline: "Prefer passing a read-only parameter by value if you’re going to make a copy of the parameter anyway, because it enables move from rvalue arguments." In fact I liked it so much that I call it "Copy and Decorate" idiom. I think it is a nice mnemonic that helps remember this guideline. In my mind it connects well with "copy and swap". You take your argument by value to get a copy of it, you decorate it, you return it. Simple.

Let's look at example often shown to show why C++11 is normally a better language than pure C.
 string compose(const string& name, const string& domain)
 {
  return name+’@’+domain;
 }
This example is a pure win compared to C solution that allocates memory and copies bytes in funny ways. It was used by Prof. Stroustrup himself in a recent interview. It can also be used to illustrate Copy and Decorate idiom:
 string compose(string name, const string& domain)
 {
  name+=’@’;
  name+=domain;
  return name;
 }

Name is copied anyway so the copy with passing it by value. Or by move. But I doubt that symbolic performance gain is worth the damage to readability. But there is more to it. This example can also be used to show the kind of tricks one may use in attempts to improve performance:
 string compose(string name, const string& domain)
 {
  name.reserve(name.size()+domain.size()+1);
  name+=’@’;
  name+=domain;
  return name;
 }

Here reserve is used to hint how much memory should be allocated. Also the function is already almost as long and as confusing as C alternative. The only difference is that in C++ error in estimation of the required size leads to poor performance, in C such miscalculation leads to memory corruption and crashes.
P.S: blogger WYSIWYG editor is horrible at formatting code. WYG is actually not WYS.

No comments: