It is a bad practice to universally label things a "bad practice" as a substitute for discussing the real problems and trade-offs.
Some techniques can be bad and are misused often. The using
and especially using namespace
directives can be misused, so a cookie-cutter mindset is to just say they are somehow "bad".
Inside a single .cpp file, you have more control over the namespace(s), so using std::cout
at the file scope or even using namespace my_funny_namespace
is fine as long as the opportunities for clashes are manageable and there is some gain in readability. At some point, perhaps if the .cpp file gets complex, you run the risk that a new name gets added to some_funny_namespace
that clashes with one of your names and using namespace some_funny_namespace
would bring in something that would break your code. But this might be less likely with something stable like boost or std.
You can also use using
locally, per-function. But if most functions in the file need the same declaration, D.R.Y.
Where it can cross the line into a really bad thing is when you use either of these in a header. Then you have leaked names from another namespace into every file that includes your header too. See this great answer for alternatives and discussion. This is really bad because it can cause spooky action-at-a-distance bugs when including or not-including a header causes some completely unrelated problem to show up or disappear. And it isn't just "your" code that might break, it could break someone else's code that uses your header.
Don't leak names into other namespaces
So using
and especially using namespace
is verbotten in headers? What if the header is for internal use within a select set of .cpp files? Then, maybe it's OK. That's the problem with cookie-cutter rules, there is always an exception and making some hypothetical thing worse for no reason but best-practices is terrible.
The upside to using
is readability (which should not be under-rated).
The downside is the potential for name clashes, especially unpredictable ones in some other random code.
Choose wisely. Carefully considering all aspects of your design is the good practice.
(and asking good questions isn't too bad either).