title: Long delegation chains in C++
body: <blockquote>
    <p>This is definitely subjective, but I'd like to try to avoid it
    becoming argumentative. I think it could be an interesting question if
    people treat it appropriately.</p>
  </blockquote>
  
  <p>In my several recent projects I used to implement architectures where long delegation chains are a common thing.</p>
  
  <p>Dual delegation chains can be encountered very often:</p>
  
  <pre><code>bool Exists = Env-&gt;FileSystem-&gt;FileExists( "foo.txt" );
  </code></pre>
  
  <p>And triple delegation is not rare at all:</p>
  
  <pre><code>Env-&gt;Renderer-&gt;GetCanvas()-&gt;TextStr( ... );
  </code></pre>
  
  <p>Delegation chains of higher order exist but are really scarce.</p>
  
  <p>In above mentioned examples no NULL run-time checks are performed since the objects used are always there and are vital to the functioning of the program and
  explicitly constructed when execution starts. Basically I used to split a delegation chain in these cases:</p>
  
  <p><strong>1)</strong> I reuse the object obtained through a delegation chain:</p>
  
  <pre><code>{ // make C invisible to the parent scope
     clCanvas* C = Env-&gt;Renderer-&gt;GetCanvas();
     C-&gt;TextStr( ... );
     C-&gt;TextStr( ... );
     C-&gt;TextStr( ... );
  }
  </code></pre>
  
  <p><strong>2)</strong> An intermediate object somewhere in the middle of the delegation chain should be checked for NULL before usage. Eg.</p>
  
  <pre><code>clCanvas* C = Env-&gt;Renderer-&gt;GetCanvas();
  
  if ( C ) C-&gt;TextStr( ... );
  </code></pre>
  
  <p>I used to fight the case (2) by providing proxy objects so that a method can be invoked on non-NULL object leading to an <code>empty</code> result.</p>
  
  <p><strong>My questions are:</strong> </p>
  
  <ol>
  <li>Is either of cases (1) or (2) a pattern or an antipattern?</li>
  <li>Is there a better way to deal with long delegation chains in C++?</li>
  </ol>
  
  <p>Here are some pros and cons I considered while making my choice:</p>
  
  <p><strong>Pros:</strong></p>
  
  <ul>
  <li>it is very descriptive: it is clear out of 1 line of code where did the object came from</li>
  <li>long delegation chains look nice</li>
  </ul>
  
  <p><strong>Cons:</strong></p>
  
  <ul>
  <li>interactive debugging is labored since it is hard to inspect more than one temporary object in the delegation chain</li>
  </ul>
  
  <p>I would like to know other pros and cons of the long delegation chains. Please, present your reasoning and vote based on how well-argued opinion is and not how well you agree with it.</p>