Home  »  ArticlesProgrammingTechnologyTips   »   Why “using namespace std” is Considered Bad Practice

Why “using namespace std” is Considered Bad Practice

Every C++ programmer whether new or old is familiar with namespaces and more so “using namespace std”. In most C++ tutorials and courses this is taught early and with good reason. It is used to reduce the typing the C++ programmer needs to do and also make the code more readable.

using namespace… allows the programmer to reduce repeated typing of the said namespace. Take for instance the using namespace std. When printing output to standard output you can use something like std::cout every time you need to print out some information to the standard output for example.

std::cout << "This is some text\n";

With using namespace std it allows you to do the following:

using namespace std
 ...
cout << "This is some text\n";

It’s that simple. At least things should be that simple but it is not always the case.

So Whats Wrong With “using namespace std”

Well, this has nothing to do with performance but more of for lack of a better term… confusion. Lets us consider this.

using namespace foo;
using namespace bar;

So from the above, we have assumed you are using two libraries called foo and bar respectively. We can assume there is a function called booboo() in the foo library and a couple of others too. In the bar library there is a function called baabaa() and a couple of other unique functions too.

Calling either of these functions works just fine in this context. Now let’s complicate things a little. You decide to upgrade to the latest version of foo with a barrage of new features and bug fixes. Unfortunately, the foo library happens to include a new function called baabaa().

You have just introduced a conflict. The new version of foo and the existing bar, both import baabaa() into the global namespace. As far as C++ is concerned, this is not acceptable and the compiler will surely complain about this. You can imagine the headaches you have to potentially deal with especially if you are working with a large codebase. The nightmare in fixing this issue will probably drive you up the wall depending on the size of the codebase.

See also  How to use WordPress Shortcodes in the Template

This could have been avoided if the original functions were called foo::booboo() and bar:baabaa(). No conflict there. You now have the option of enjoying the benefits of foo::baabaa() too without the risk of complicating your code.

Be sure to avoid “using namespace” as much as possible. If you do use it and are aware of existing conflicts you can explicitly call the foo::baabaa() and bar::baabaa() where appropriate.

Another way to gracefully handle this is to use specific tokens such as this:

using std::cout rather than using namespace std if you are going to use a lot of cout and know it will not cause conflicts.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Share it!

Available under:
Articles, Programming, Technology, Tips