Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices: using namespace or reopen namespace? [closed]

Assume I've declared a function (or class, doesn't matter) in a header file, which is part of namespace foo:

namespace foo
{
    void bar();
    …
}

For a long time I've been reopening the namespace when I was defining the function in a cpp file:

namespace foo
{
void bar()
{
    doSomething();
    …
}
}

That is because I learned it this way and it was used in a project I was working on. I never really though about it until recently, when I stumbled upon a project which used the using directive instead:

using namespace foo;

void bar()
{
    doSomething();
    …
}

Finally there's an option of using the full name. I find it pretty tedious, especially when classes with a lot of members are involved. In my opinion it doesn't make much sense when all content of the file is a part of one namespace.

void foo::bar()
{
    doSomething();
    …
}

So my question is which one should be preferred and why? Especially regarding the first two options (using directive vs. reopen namespace).

like image 793
stativ Avatar asked Jun 07 '12 08:06

stativ


People also ask

Is it good practice to use namespace?

The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.

What are is difference between using namespace directive and using the using declaration for accessing namespace members?

using directives Use a using directive in an implementation file (i.e. *. cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace.

What are the benefits of using namespaces?

The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features.

What can I use instead of namespace std?

The alternative is to write std:: everywhere.


2 Answers

I think the cleanest way is re-opening the namespace, and I've got the arguments to support it:

  • with your second option, with the using directive, it isn't clear that you're implementing a method in that namespace. You could as well be implementing a free function that uses something from the namespace.
  • the third option is usually used for implementing class member functions. If you look directly in the cpp file, it isn't clear that you're implementing a function from a namespace unless you know that namespace exists. The first thing that comes to mind is that you're implementing a class member function.
  • the first one is the clearest. You open the namespace and define a function inside it. The function is part of the namespace, and this is the implementation.
like image 171
Luchian Grigore Avatar answered Sep 22 '22 13:09

Luchian Grigore


Even though using namespace is the laziest (and therefore the most tempting) solution, it's often not a good idea. Besides what Luchian says about function declarations being ambiguous (someone new to the project wouldn't know if that is standalone function or one in the namespace), and the fact that you might introduce a name in the namespace later, clashing with one you are using now, I have another reason why I'd suggest using the third method.

Using the third method, you give your code more consistency. If A is inside B, you would always define it with A::B. If A is a class and B a function in the class, you would write type A::B(args). If A is a class and B a static member, you would again write type A::B = value. Now A is a namespace, but it's still the same concept: B is defined inside A, therefore it is more consistent to again use A::B.

(There is an added bonus of search-ability if your editor is e.g. ViM)

like image 21
Shahbaz Avatar answered Sep 23 '22 13:09

Shahbaz