title: Class VERSUS namespace, OR class AND namespace?
body: <p><strong>Both Class and Namespace?</strong></p>
  
  <p>This question is about a pattern that I am seeing myself use more and more: Having both a class and a namespace for related concepts.  I think this is motivated mainly by C++ language artifacts, but not totally.</p>
  
  <p>I suppose the top level question is: Is this a good idea?  Having both a class and a namespace for related concepts?</p>
  
  <p><strong>Lower level question:</strong> </p>
  
  <p>What is the best way to do this?  </p>
  
  <p>Class nested within the namespace?:</p>
  
  <pre><code>namespace Foo_Namespace {
     class Foo_Class {
         ...
     };
  }
  </code></pre>
  
  <p>Or separate, peer, class and namespace?:</p>
  
  <pre><code>class Foo_Class {
      ...
  };
  namespace Foo_Namespace {
     // non-class free functions, etc.
  }
  </code></pre>
  
  <p>I must admit that I lean towards nesting the class within the namespace.
  Even though it leads to ugly names.
  But even if I do that, what naming conventions should I use:</p>
  
  <p>The following is too long, leading to really ugly names Foo_Namespace::Foo_Class</p>
  
  <pre><code>namespace Foo_Namespace {
     class Foo_Class {
         ...
     };
  }
  </code></pre>
  
  <p>It isn't necessary to use any suffixes or indicators in the name:</p>
  
  <pre><code>namespace Foo {
     class Foo {
         ...
     };
  }
  </code></pre>
  
  <p>But then I find myself uncertain, when I look at Foo::bar(), if that is a free function bar in namespace ::Foo, i.e. ::Foo::bar(), or a member function in class Foo in namespace ::Foo::Foo::bar().</p>
  
  <p>And names like ::Foo::Foo::bar are still not, umm, nice.</p>
  
  <p>Currently I am doing</p>
  
  <p>It isn't necessary to use any suffixes or indicators in the name:</p>
  
  <pre><code>namespace Foo_ns {
     class Foo {
         ...
     };
  }
  </code></pre>
  
  <p>mainly because I usually create the class first, and then later realize that a namespace would be nice.</p>
  
  <p>I wonder if I should revive a naming convention I haven't used in years: _c for class, _ns for namespaces:</p>
  
  <pre><code>namespace Foo_ns {
     class Foo_c {
         ...
     };
  }
  </code></pre>
  
  <p><strong>Details:</strong></p>
  
  <p>I won't repeat what I have said above, but I'll add a bit more.</p>
  
  <p>The most practical reason I know of to use a namespace in addition to a class is that you are allowed to do forward declarations of free functions in a namespace, but you are not allowed to do forward declarations of some methods of a class.  I.e. with a class you have to declare it all or nothing.  Whereas with free functions in general, and free functions in namespaces in particular, you can declare it piecemeal.  Parts can be declared in different header files.  Rather than #including a massive header-only-library for a massive class, you can use forward declarations for just one or two functions.  Etc.</p>
  
  <p>(See, for example, <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Forward_Declarations">http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Forward_Declarations</a>, although Google netly comes down against forward declarations rather than including a header.)</p>
  
  <p>Another reason is that a namespace allows the class itself to be kept small.</p>
  
  <p>The biggest advantage for a class is that a class can be passed to a template, whereas a namespace cannot be.</p>
  
  <p>I prefer to have the class nested within the namespace, Foo_ns/Foo_ns::Foo_c rather than to have them as peers Foo_ns/Foo_c, because often a class needs helper classes, e.g. Foo_ns/Foo_ns::Foo_c/Foo_ns::Foo_Helper_c.  If the class and the namespace are peers, it seems strange to have Foo_ns/Foo_c but Foo_ns::Foo_Helper_c.</p>
  
  <p>I like namespaces, because I agree with Andrei Alexandresciu: <a href="http://laser.inf.ethz.ch/2012/slides/Alexandrescu/1-C++%20course%20parts%201%20and%202.pdf">http://laser.inf.ethz.ch/2012/slides/Alexandrescu/1-C++%20course%20parts%201%20and%202.pdf</a></p>
  
  <pre><code> Class methods vs. free functions
  
   • Conventional wisdom: methods are cool, free functions are so 1960s
   • Yet:
     ◦ Free functions improve encapsulation over methods
     ◦ Free functions may be more general
     ◦ Free functions decouple better
     ◦ Free functions support conversions on their left-hand argument
  
    Surprising fact #2
  
      Making a function a method should be
      your last, not ﬁrst, choice
  
    (c) 2012– Andrei Alexandrescu. 32 / 59
  </code></pre>
  
  <p>It's better to create free functions than methods in classes.</p>
  
  <p>But sometimes classes just have to be used - e.g. for templates.</p>
  
  <p>Naming convention wise</p>
  
  <p>I used Foo_ns / Foo_ns::Foo_c in the past</p>
  
  <p>I'm using Foo_ns / Foo_ns::Foo now</p>
  
  <p>(By the way, I tend to use Class_Names_With_Underscores_and_Initial_Caps, not CamelCase.)</p>
  
  <p>If it makes sense, I might elide the _ns suffix on the namespace - e.g. where the namespace and the class(es) do not need to have the same name.</p>
  
  <p>I dislike making them having the same name.  Consider a constructor for a class Foo inside a namespace foo:</p>
  
  <p>::Foo::Foo::Foo()
     vs
     ::Foo_ns::Foo::Foo()</p>
  
  <p>The latter is not much better, but is a bit less confusing.</p>
  
  <p>I think that I usually create the class on its own, without nesting it in a namespace.
  In fact, I probably add a few static methods, before I realize that a class nested within a namespace would be better.  By that stage it may be a pain to refactor, and I sometimes end up creating forwarding functions, that forward from the class static method to the free function in the namespace, or vice versa.  This causes me to regret bot to jump to class wthin namespace right from step 1.</p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>My current BKM is Foo_ns / Foo_ns::Foo, i.e. </p>
  
  <pre><code>namespace Foo_ns {
     class Foo { 
     ...
     };
  }
  </code></pre>
  
  <p>I'd appreciate any suggestions, any improvements.</p>
  
  <p>Or am I just broken for doing this?</p>