Avoid Using Namespace

“using namespace std” is a BAD practice in C++

Mayank Khanna
3 min readApr 29, 2019

Yup, you got it right. The line of code we are so addicted to and is often considered as a common practice is actually a bad practice and here is why so. So considering the fact that we most often need to access many class or types declared in the standard namespace, and for that we need the namespace std, it actually imports all of the std namespace into our current namespace. And this is actually not good at all. Let’s see how.

Simple and common code we write

Now the above code, is quite simple and will work fine. But if we have another header file containing some other functionality of standard class cin or cout, it will give us an error. Here we have an ambiguity, that to which cin or cout does the compiler needs to access ?! Here the compiler will may detect this and not compile the code. If we are really unlucky, it may compile the code and call the wrong function, since we never specified ed to which namespace the identifier belonged.

Functions and Classes in demo.h can cause ambiguity

Namespace were introduced in C++ to resolve identifier name conflicts, so that 2 objects can have same name and yet treated differently if they have different namespaces. And as a matter of fact, what the namespaces were introduced to prevent is what they are causing, conflict ! When we import a namespace we are essentially pulling all type definitions into the current scope. Keeping in mind the size of the std namespace (its very HUGE !), we need to avoid the use of namespace std and find a solution to this. And OH ! we have a solution.

Solves the problem but messes up the code

Yes the solution to the above problem is using the keyword std::. Although namespace std helps us avoid writing this keyword again and again, we don’t need to discuss more about the harm it may cause in case of using 2 classes having functions with same name. Although you may think we have our problem solved, but its just the beginning. The scope operator (::) and std keyword solve the purpose but also makes the code look not so user-friendly and difficult to read. Considering an example for the same :

Code to get current time in program

I really like how using the std and scope operator (::) has made look like a total mess and is not at all good for the health of new programmers and end users to understand. Well we all need another solution for the same, because we know if we save ambiguity, its gonna cost us clean and tidy code. Well in no time do we have our another shining solution ready. Yup, it is the use of typedef. It saves us from writing long type definitions, and makes our code look pretty. We can make different typedefs for different class or headers.

Typedefs pretty much solve our problems

Well now that we have a nice and tidy solution, I can discuss some tips or other ways for the same solution. For example instead of importing the entire namespace, we can simply import the truncated namespaces. This is only good if you know which class or type you need to access. For example using namespace std::chrono . This can solve our problem and we can now just simply use the functions in the chrono class.

Import in a function instead of globally

And if you are really a rebel, and are determined to use the entire namespace and import it , try to do so inside the scope of the function. Try this approach because even if we import the entire namespace, it will only be valid in the scope of the function and will avoid the ambiguity most of the times. So I guess we now know all the possible alternatives of using or not using the namespace std . And in all we can determine is that we must avoid importing the entire library.

--

--

Mayank Khanna
Mayank Khanna

Written by Mayank Khanna

I love writing and expressing in the form of poem, articles and stories. I’m an SRE by the day and a writer by the night.

Responses (2)