Friday, November 20, 2009

using namespace std; in C++

Lately I have been into C++ quite a bit, I've been reading some books and this Fall 2009 semester I'm taking a Computer Science class at Red Rocks, all about C++. The language, and the way that it works cannot function without certain standards, otherwise you get something like HTML where if you forget to include a closing tag (like but no ) then it still works, but if you forget those tags, somewhere down the line you will have a huge problem and you can't figure out WHAT the heck is going on. Well C++ has it's standards, something called ISO http://en.wikipedia.org/wiki/ISO/IEC_14882#Language_standard but this set of coding standards doesn't include certain things that could be vital to your program running correctly or not.

A particular problem that I have found, is the statement “using namespace std;” What this does is opens up the entire scope of code you place it in to the standard library, and it restricts the names you are possibly able to use. For example, did you ever know that copy, list, sort, and max are all std names? Once you include the 'using namespace std;' at the top of your file, you've added all of those functions to the global namespace and you can't ever make a variable or function with that name anywhere in your code. So this doesn't seem so bad, worst case scenario you just go and change those names. It may take you a little while (a long time if your code is long at all) to find out what went wrong, and you might not receive an error message, but you can just go back and use different names. So now you go get a library using a different namespace, called nonstd so at the top of your source file you have 'using namespace std;' and 'using namespace nonstd;' We've already deduced that the std namespace has a function called copy, but this nonstd namespace also has a function called copy. It will be hell to find out what is going wrong.. and further more how will you fix it?

Okay, so it's true that anyone who is able to write their own library and publish it so you can use it will be careful NOT to ever use any names that conflict with the std namespace, but that's what namespaces are for! One useful thing about namespaces is that two different libraries can use the same name without being conflicting. The thing is the names will conflict if you use the 'using namespace std;' declaration in your source file. While I was researching this topic, I found this: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c Here, sbi answered a question, scroll down a little to see it: “It took most of us very few weeks to get to used to write the prefix and after a few more weeks most of us even agreed that it actually made the code more readable.” Here he was talking about a project where it was agreed that using directives would not be used at all, and this is the result of that.

So what is the solution to not using the using-directives? Well the simple way is to type std:: in front of everything, as so: std::cout, std::cin, std::endl, etc. But that takes a little effort. Better than using namespace is using std::cout, which solves a couple of the problems I talked about previously. However, if you do run across two namespaces with the same name, and you need to use both names in your code, then you will have to type the namespace::function.

So as a short conclusion, don't use 'using namespace std;' in your code, it has some bad effects in the long run. In small projects for a beginning C++ class at school it's probably okay since you usually won't write any excessively long source files with lots of naming conflicts.

No comments:

Post a Comment