+ 9

Why is it not advisable to write "using namespace std"?

I have oftentimes heared that its not a good practice to write "using namespace std;" in a C++ program. Why is it so?

18th Feb 2019, 12:52 PM
Abhishek Tandon
Abhishek Tandon - avatar
25 ответов
+ 13
Because there are a lot of names in std and by using all of them everywhere and at all times, you can lose track (in a longer code). For example if someone reads your source, it can be troublesome to figure out where a name that is used in the code even comes from. If you explicitly access what you need and nothing more, it's easier to know what belongs where. You could write: using std::cout, std::cin; if that is all you use anyway. Then every name is explicitly 'declared'. Or you use using namespace std (when needed) only in the context of a function, locally, where it's easy to oversee who has access to it (just the function).
18th Feb 2019, 1:49 PM
HonFu
HonFu - avatar
+ 13
AZTECCO I'm curious to see your response as well. I think this FAQ on isocpp.org covers all the talking points for this question as well. https://isocpp.org/wiki/faq/coding-standards#using-namespace-std
18th Feb 2019, 4:45 PM
David Carroll
David Carroll - avatar
+ 9
David Carroll Thank you so much sir for the article. Its really helpful :) Btw I am ROFL after reading this about pointer casts in this article 😂 "I think of them as a filter on error messages: the compiler wants to complain because it sees you’re doing something stupid, but it also sees that it’s not allowed to complain due to your pointer-cast, so it drops the error message into the bit-bucket. It’s like putting duct tape on the compiler’s mouth: it’s trying to tell you something important, but you’ve intentionally shut it up."
18th Feb 2019, 5:30 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 9
Nobody quoting Ace? What is this. >:c https://www.sololearn.com/Discuss/294730/?ref=app
19th Feb 2019, 11:28 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
AZTECCO I have to disagree with your statement: "... this article says exactly what I say." You've stated multiple times that it's a bad practice to bring specific functions into the local namespace with the using ~directive~ declaration. (i.e. using std::cout) However, the article states it's fine as long as it is known to not collide with another namespace and that it is consistent within your team. It is probably more suitable to state it's considered a bad practice on your team. However, the specific reason is what remained missing from your explanation. In the end, it comes down to a matter of preference. For me, personally, I prefer explicitly typing std::cout instead of cout. However, I do so, not because it's a good practice. Rather, it's my preference. Also, it's an irrelevant argument to state that you aren't the only one who believes this. This argument suggests either a lack of deeper understanding or the understanding is, again, a matter of opinion.
18th Feb 2019, 7:24 PM
David Carroll
David Carroll - avatar
+ 6
19th Feb 2019, 11:40 AM
Abhishek Tandon
Abhishek Tandon - avatar
+ 5
Because you can run in trouble with namespace clashes! If you have multiple namespaces with the same function your code doesn't know which to use. And for readability too. When you write small codes here is all fine but in real programming not. TIP: you can set your keyboard to output std:: when you press one specific button.
18th Feb 2019, 1:49 PM
AZTECCO
AZTECCO - avatar
+ 4
AZTECCO Had I known that a website called isocpp containing this information exists, there was no point in asking this question here. We learn from each other. There are tons of websites out there on the internet and one can't keep track of each and every website.
18th Feb 2019, 7:54 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 4
For smaller programs, there is no harm using it. I am steering well clear of the debate on its use in larger programs.
19th Feb 2019, 8:07 AM
Sonic
Sonic - avatar
18th Feb 2019, 1:36 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 2
Well, the point of having datatypes, functions, classes and all that 'encapsulating' stuff, is handling complexity, right? Prevent name clashes, reduce the scope of error, increase maintainability. So everything that counteracts that by opening up the scopes again, by creating side effects from functions, using global variables, keeping stuff in classes generally public and accessing it haphazardly, could be considered 'bad' in this context. I can see by writing 'using namespace std;' right at the top of your code is problematic from that perspective. On the other hand I don't understand, how 'using std::cout' in a local context, let's say in a method that fits in one or two screens, could be reasonably called 'bad practice' .
18th Feb 2019, 4:10 PM
HonFu
HonFu - avatar
+ 2
😂😂😂 Infinity ♾️ What an explanation!!
18th Feb 2019, 5:36 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 2
Infinity ♾️ first thing: iso cpp ,cppreference and stack overflow are really important, you will visit them all the time, after comes geeks for geeks and others.. but the first 3 are really essential . I'm glad you've discovered them.
18th Feb 2019, 8:04 PM
AZTECCO
AZTECCO - avatar
+ 2
I don't blame you Infinity ♾️ You learned something really useful and that's great.
18th Feb 2019, 8:47 PM
AZTECCO
AZTECCO - avatar
+ 1
using "using std::cout" is considered a bad practice too and the same for context
18th Feb 2019, 2:28 PM
AZTECCO
AZTECCO - avatar
+ 1
What specifically makes 'using' a bad practice when you don't abuse it to create something like a global namespace of the complete stdlib?
18th Feb 2019, 3:04 PM
HonFu
HonFu - avatar
+ 1
Your local context example is fine, it's not to avoid like the rest, but it's considered bad practice, not just from me eh.. Why? For the sake of simplicity.. that's all.. I told you before what means abuse or something like? I knew you exactly you knew what you were referring to.. But is too complicated. using always xxx::yyy is simple and clear and doesn't cost much. define when to use one style over the other is complicated, and code could be changed. So if I give an advice is to use always std::xxx This doesn't mean your adding is wrong.
18th Feb 2019, 4:58 PM
AZTECCO
AZTECCO - avatar
0
Really, AZTECCO? Even if you do it non-globally?
18th Feb 2019, 2:29 PM
HonFu
HonFu - avatar
0
Yes, it's safe but considered bad practice.
18th Feb 2019, 3:01 PM
AZTECCO
AZTECCO - avatar
0
What specifically means 'abuse' and 'something like'? You got the answer then.
18th Feb 2019, 3:31 PM
AZTECCO
AZTECCO - avatar