Why 'using namespace std' is bad practice

In class, we always use this, but every time I watch a tutorial or see references to something else online, they use std::cout. I decided to look it up and only recently learned it was bad practice, and it makes perfect sense.

The problem is not actually the namespace, but rather the library. Remember that we’re using the library called iostream, which has pre-written functions such as cout and cin. Realistically, we’ll end up including several libraries whether it’s for graphics or mathematical functions or other things. The problem is that somewhere in those libraries, there may be a function called cout or cin. If the same function exists in multiple libraries or namespaces, the compiler won’t know which one to use. However, by using std::cout_ we’re specifying the cout method in the std namespace.

4 Likes

sounds good.!How would you define namespace? What is it?

An interesting thing about learning programming are the people that teach it using bad practices or habits that you have to unlearn. Programming already requires a lot of wrapping your head around things that it shouldn’t be added to.

How would you define namespace? What is it?

A namespace is exactly how it sounds. Its a space for names. This way you can give namespace to this library or that library and it won’t conflict with the other. You probably won’t need it in small simple applications unless you use 3rd party code (libraries) or whatever and wish to use the same names without affixing them or whatever.

There are certain times when you will need or want to use it and then the rest of the time you won’t ever need to care.

Once you wrap your head around it, its no big deal. More annoyance than anything.

For comparison, Java does namespace differently to the point that you don’t even need to think about it. So much better than c++.

From my understanding it is still bad practice in smaller projects, as it may carry on to bigger projects where the namespace clashing can happen. Right?

If you want to always work to have best practice for working on large projects, libraries, etc then yes, always keep everything so names don’t clash.

It will be overkill on some projects where it doesn’t make a difference, but if that’s what you want to do then you certainly can.

1 Like

using name space can cause clashes if there are more then one object in the library with the same name

If you call another library you may end up in a namespace clash. That will cause you issues down the line. I would suggest using std:: instead of namespace, because that way you may search the library of your choosing? I am new to this.

1 Like

Privacy & Terms