Section 2 Lecture 13

If I understood correctly:

  • Benefits of using namespace std: gets you out of having to write :: repetitively, and cleans up your code for easier reading.

  • Drawbacks of using namespace std: run the risk of namespace conflicts if your code uses a function defined in two or more libraries you are using, or in two or more namespaces within a library used in your code.

Found the following discussion about this topic informative: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice.

6 Likes

Yep, that pretty much covers it.

Another trick you can use is only specifying the specific functions that you want. This is a good way to remove the repetition of a small number of commonly-used functions, such as cout, while avoiding some of the risks of namespace conflicts.
Just replace “using namespace std;” with “using std::cout;” (and “using std::endl”)

Another thing to keep in mind is that your code will be written once (OK, probably a few times, as you go back and refactor things and fix bugs!), but likely read many many more times by yourself and others. If we look at it like this, then explicitly declaring “std::cout” everywhere it’s used can actually be seen as an enhancement to readability. cout is a trivial example, but if you extend your thinking to consider cases of less-common functions from a slew of 3rd-party libraries that not everyone has memorized, you can start to understand the value of clarity.

Spending a little extra time to write the code will pay dividends over time. The UE4 style guide alludes to this principle in a few places, such as “Classes should be organized with the reader in mind rather than the writer.”

4 Likes

The risk of ‘using namespaces’ is that you don’t know what code the compiler is implementing into your program, if the function or variable in the namespace exsist also in another space of your program or includes. If your program has some weird behaviors it is difficult to find the source of the problem.

Hello,

Well I think I understand, well I understand that the same function such as “cout” can exist in different libraries (right?).

I created a new discussion to ask more questions about this.

Thanks

After the lesson I understood the conflict that can arise from “using namespace std;” if a function of the same name is present in multiple libraries.

However, after reading your comment I think I understand it from a new and very practical perspective. I had not considered the importance of being able to rereading your own code or someone else’s. I can see why it would be helpful to make it clear exactly which function you are using each time.

Thanks

I think the discussions above cover just about everything that comes with “risk” and “benefit”.

Personally and professionally I’ll add that “using namespace std” or any declaration of “using” should be limited to explicit calls only. Example "Using std::cout"
And even then it would be recommended to avoid doing so. Yes it’s annoying to have to keep typing the namespace qualifier (std::). But as the zen of programming goes; “Explicit is better than implicit”

A word of warning to all learning C++ for the first time, never create abstraction for the sake of abstraction. In other words, write code that’s easy to understand and read. And NOT code that’s easy to write.

After going through the lesson, It seemed to make most sense to continue to declare the library through “std:::” rather than “using namespace std;”. Although you may not always be encountering situations where multiple libraries with similar commands will be used in the code, it’s a good habit to get into using the “::” method.

So here is the deal.

Using namespace is a convenience. However, in the context of teaching, and learning, it has so many draw backs, I think it’s not worth mentioning.

People are very familiar with lumping things into boxes. Whether it’s politics, their jobs, or even education, there are boxes/patterns, and people are constantly putting things into them.

When you teach people to put using namespace std, they immediately go into pattern recognition mode. They learn, as I did in college, that you simply put that at the top of any main file. This will be good for them in the vast majority of cases, but it is incorrect. When they hit that wall, and they will, they will have a huge wtf moment.

I feel it’s better to explicitly state the namespace, that way you are teaching them that these things are in a specific “box”. Maybe later, down the road, teach them about using. You should teach them the way first, then the conveniences later. You learn how to program a linked list, before learning about including the list library. The same should be true for using. Teach them about the “boxes” first, then teach them the short cut.

Just my two cents. Having said that, I would prefer people be coding in any way, than not at all. So even if you learn using right off the bat, I salute you. Keep at it.

Basically how I understand it:

Using namespace allows you to not have to type std:: over and over again to identify which namespace you’re using. Which is great.

However if you are using multiple libraries or even sometimes the same, both libraries or namespaces can have the same name for different things which will majorly conflict with your code.

Privacy & Terms