Whenever one sees example C++ code basically anywhere, be it
in a book, in a tutorial in the internet, in an online forum
or whatever, I would estimate that at least in 99% of cases one
sees the use of "using namespace std;" to get rid of that
namespace. In fact, "using namespace ..." is very popular with
all documentation and example code of most C++ libraries out
there which use their own namespace.
This raises the question why use namespaces if people are
going to get rid of them anyways?
Ok, let me take that immediately back. That's not actually what
I wanted to ask. What I wanted to ask is the opposite: Why do
people always get rid of all namespaces without giving a second
thought to *why* those namespaces exist?
There's a reason for namespaces to exist and for them to be used.
Why are people so eager to get rid of them? This is especially
curious with libraries which use their own namespace (let's say,
for the sake of an example, "boost") and in all their example codes
they nevertheless always get rid of it ("using namespace boost;").
One reason I have heard is that people think that having to always
write the namespace prefix before all the names is laborious and makes
the code unclear and hard to read. I think this couldn't be farther
from the truth.
Personally I made the decision many years ago that I will never
use "using namespace" to get rid of a namespace. I quite quickly
got accustomed to always write "std::" before all C++ standard types
and function names and such. Sometimes when I make a library I use
my own namespace name and always put the prefix when I use anything
from the library.
I quickly noticed that, instead of making the code unclear,
it actually made the code more readable! Once you become familiar
with the namespace prefixes it actually makes it easier to understand
the code. It makes many things unambiguous and quicker to understand.
For example, if I see the name "sort" in some code, there may be
some doubt about whether that's actually C++'s own "sort" or if it's
something the code defines elsewhere. However, if I see "std::sort"
instead, it's *immediately* crystal clear what it is. There's just
no doubt.
I think people have prejudices against the namespace prefixes.
They see - they *want* to see - annoying extra garbage characters.
I think this is a question of attitude. If people approached
namespaces with a more positive attitude I believe they too would
start seeing them as something which *helps* and makes code clearer
than something which is an annoying nuisance.
I think namespaces should actually be used more than they already
are. This may be especially useful for people reading other people's
code. When I read someone else's code I often have to spend a big
amount of time trying to resolve what each function or type name
means, where it is defined and what it does. Namespaces could help
with this: If a big program consists of many independent components
which could be considered their own "libraries" inside the program,
serious thought should be put into putting them into their respective
namespaces, and each time what they define is used, the proper
namespace prefixes should be used. This way someone reading the code
will more easily know where to look when he wants to know what some
function or type is.
IMO "using namespace" has no use in C++ and only causes lazy people
to write unclear code. I recommend anyone who asks to avoid writing
it altogether. The first few hundreds of lines of code might feel a
bit awkward, having to write all those extra "std::" prefixes, but
I assure that it very quickly becomes like a second nature to write
them, and they very quickly stop looking like garbage and instead they
become the exact opposite.