Why is "using namespace std" considered as bad practice? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is "using namespace std" considered as bad practice?

25th Jun 2017, 8:05 AM
ANAM NAUSHAD
ANAM NAUSHAD - avatar
4 Answers
+ 3
It is considered bad practice, actually. You do not want to use this in production code. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
25th Jun 2017, 10:07 AM
Dennis
Dennis - avatar
+ 2
Because of naming collisions and polluting the global namespace
25th Jun 2017, 11:27 AM
aklex
aklex - avatar
+ 2
the reason it is considered bad practice is it is prone to errors and can cause issues with your compiler/program, especially when you use a function, class or whatever that is declared and defined in multiple namespaces. a good practice is to avoid it in header files all together and to limit the scope of the namespace you are using, or to call the object itself that you need to use. <bad practice>: //header. hpp using namespace std; class someClass{} ; <okay pratice>: //source.cpp using namespace std; //code here <better practice>: //source.cpp int add() { using namespace std; } <even better practice>: //source.cpp #include <iostream> using std::cout; <best practice>: //source.cpp #include <iostream> int someFunction() { using std::cout; cout << "something" ; } the above is ideal when using the same object multiple times. if you are using it once only, it'll be better to use: //source.cpp #include <iostream> int someFunction() { std::cout << "something" ; } these ways if someone comes along and creates a new namespace with a function that has the same name as one you defined, you don't have to alter your whole program to cater for the change. i hope this is clear enough, if not message me or something.
25th Jun 2017, 11:36 AM
Anthony Mans
Anthony Mans - avatar
0
Its not? Who said it was?
25th Jun 2017, 9:50 AM
Shawn
Shawn - avatar