title: Where to put helper functions pertaining to class method in C++
body: <p>Suppose I have a class <code>Foo</code> like this:</p>
  
  <p><strong>foo.h</strong>:</p>
  
  <pre><code>namespace mine {
  
  class Foo {
    Widget widget_;
  public:
    void bar();
    // some other members...
  };
  
  } // namespace mine
  </code></pre>
  
  <p><strong>foo.cpp</strong>:</p>
  
  <pre><code>#include "foo.h"
  namespace mine {
  
  void Foo::bar() {
    // Some very long code
  }
  
  } // namespace mine
  </code></pre>
  
  <p>where I want to split <code>bar()</code> into multiple functions for readability reasons. The functions themselves don't have any particular meaning to <code>Foo</code> (or any other entity than <code>Foo::bar()</code>) and are only used to split up <code>bar()</code>, so according to <a href="https://softwareengineering.stackexchange.com/questions/134448/where-should-i-put-functions-that-are-not-related-to-a-class/134452">this</a> discussion I would do the following in the source file:</p>
  
  <p><strong>foo.cpp</strong> (refactored):</p>
  
  <pre><code>#include "foo.h"
  
  // anonymous namespace to put all helper functions
  namespace {
  
  void computeResult() { ... }
  void modifyWidget(Widget&amp; w) { ... }
  void doThis() { ... }
  void doThat(Widget&amp; w) { 
    // ... 
    modifyWidget(w);
  }
  
  } // &lt;anonymous&gt; namespace
  
  // actual methods are defined here    
  namespace mine {
  
  void Foo::bar() {
    ::doThis();
    ::doThat(widget_);
    ::computeResult();
  }
  
  } // namespace mine
  </code></pre>
  
  <p>So I am defining an anonymous namespace in the source file in order to define the helper functions, such that I have static linkage and the helper functions are not visible from outside the source file. One thing that looks odd to me is that class methods depend on functions that are not part of the class, but then we would not be able to use even the standard library if this was an issue.</p>
  
  <ul>
  <li>Is this approach sensible? Do you have better suggestions? </li>
  <li>Is there a problem with passing the private member <code>Foo::widget_</code> to some freestanding function that modifies it (<code>doThat()</code>)? I'm assuming here that in the narrow context of a static linkage helper function, the callers/callees know what they are doing.</li>
  </ul>