tags:

views:

304

answers:

4

Assuming a class called Bar in a namespace called foo, which syntax do you prefer for your source (.cpp/.cc) file?

namespace foo {
...
void Bar::SomeMethod()
{
    ...
}

} // foo

or

void foo::Bar::SomeMethod()
{
    ...
}

I use namespaces heavily and prefer the first syntax, but when adding code using the Visual Studio Class Wizard (WM_COMMAND handlers, etc.) the auto-generated code uses the second. Are there any advantages of one syntax over the other?

+1  A: 

I think it depends on each case. If it's a 3rd party library that's similar to your own code, and you want to avoid confusion in the code, then specifying the full namespace at each occurance may be beneficial. But other than that, less code is generally better (unless it becomes unclear).

GeekyMonkey
+4  A: 

I would decline the first (edit : question changed, the first is what i prefer too now). Since it is not clear where Bar refers to from only looking at the function definition. Also, with your first method, slippy errors could show up:

namespace bar { 
     struct foo { void f(); };
}

namespace baz { 
    struct foo { void f(); };
}

using namespace bar;
using namespace baz;

void foo::f() { // which foo??

}

Because it looks in the current scope (there it is the global scope), it finds two foo's, and tells you the reference to it is ambiguous.

Personally i would do it like this:

namespace foo {
void Bar::SomeMethod() {
    // something in here
}
}

It's also not clear from only looking at the definition of SomeMethod to which namespace it belongs, but you have a namespace scope around it and you can easily look it up. Additionally, it is clear now that Bar refers to namespace foo.

The second way you show would be too much typing for me actually. In addition, the second way can cause confusion among new readers of your code: Is foo the class, and Bar a nested class of it? Or is foo a namespace and Bar the class?

Johannes Schaub - litb
+4  A: 

I prefer an option not listed:

namespace foo {

void Bar::SomeMethod()
{
    ...
}

}  // foo namespace

Unlike option one, this makes it obvious your code belongs in the foo namespace, not merely uses it. Unlike option two, it saves lots of typing. Win-win.

hazzen
This is what I meant for option 1.
Rob
+1  A: 

I'd rather go for the first case, where namespaces are explicitly marked:

namespace TheNamespace {
void TheClass::TheMethod() {
   // code
}
}

The reason is that it is clear from that syntax that TheClass is a class and TheNamespace is a namespace (not so obvious by any other names). If the code were

void TheNamespace::TheClass::TheMethod() {
   // code
}

then a reader does not clearly see whether TheNamespace is a namespace, or a class with an inside class by the name of TheClass.

class TheClass1
{
   class TheClass2
   {
      void TheMethod();
   }
};

void TheClass1::TheClass2::TheMethod() {
   // code
}

I tend to avoid the 'using' statement, and even then the example by litb with two classes in two different namespaces will make both the compiler and the programmer confused and should be avoided.

David Rodríguez - dribeas