using namespace

Now that we have unified builds, writing "using namespace" in the global
scope of a .cpp file is almost as bad as writing it in a header. Regularly
build errors show up like this one:

What's awesome about these errors is that they typically show up on some
platform but not the others and they come and go as we add files or do
things that can alter the generation of the unified .cpp files.

So, I strongly encourage everyone to stop using "using namespace".
If you really really want to use them, please put them inside the scope of
a namespace. For instance a .cpp file in the gfx/layers directory typically
looks like this:

// using namespace gfx; here means nical: r-
namespace mozilla {
namespace layers {

// using namespace gfx; here means nical is grumpy but he'll probably not
block your super important path for this.

// ...


Beware that once using namespace is placed in an enclosing namespace foo,
it will affect the namespace foo the next time you open it:

namespace foo {
using namespace unicorns;
// ...

namespace foo {
// here it's just like you wrote using namespace unicorns; again

Which leads to problems with namespaces like mozilla::ipc and
mozilla::dom::ipc being ambiguous in the mozilla namespace. it is probably
just not possible to write using namespace mozilla::dom; in most of the
layers code for this reason.

Honestly, I don't mean to start a bikeshed about whether we should enforce
strict rules about this project-wise, because I know that people have
different tastes as to whether writing "Size" is vastly better than
"gfx::Size". But I'd like to encourage people to carefully make use of this
c++ feature, especially when the namespace in question is used a handful of
times in the file. I personally will be strict about it the reviews I give.


5/20/2014 4:36:13 PM 6651 articles. 0 followers. Post Follow

0 Replies

Similar Articles

[PageSpeed] 19