Why do all C++ Puzzle use "using namespace std;"

Hello,

I noticed that the C++ puzzle start code uses
using namespace std;.
This encourages coders to adapt bad practice.
If the aim is to make the start code more readable, one could always got for
using std::cout; using std::cin;
Have I missed something, is there more to it?
I’ve tired the search and couldn’t find a thread about it. Plus I’m not sure, if I picked the right sub forum.

Cheers Tofu

Hi Tofu,

You’ve got the right forum. :slight_smile:
Any using namespace is not bad practice per-se. It’s only bad practice if you put the using statement in a header file. In cpp files, it’s perfectly acceptable. Unless you type using namespace std; and using namespace boost;. That almost guarantees name clashes.
Personally, I’m not a big fan of the using statement for every item in a namespace. That’s just clutter.
If you’re against using namespace, then namespace aliases can be a nice alternative. A popular one: using fs = boost::filesystem;

On codingame, I’d stick with using namespace std; It’s just one file anyway, so as long as it compiles, it’s fine.

1 Like

Actually for coding-competition-type problems it makes more sense to just write using namespace std;. In competitions hosted by professional organizations you typically aren’t going to be using multiple namespaces (STL is usually allowed while boost is not), and under a time limit writing out the scopes seems sorta pointless.

I’d argue that CodinGame should not teach beginner programmers to put using namespace std; even in small files it might result in namespace pollution, and coding standards like the Google coding standard forbid using namespace.

It might not be a big problem in a small file, but that’s not a given and you should definitely not get used to it.

That’s a pretty subjective thing to say. I personally like using when used properly. Like having a common namespace for a dll and adding a using of that namespace in the cpps of the dll.

Personally, since std is pretty much part of C++ itself, I don’t see any problem with that using either. And I mean… for teaching “bad practice”, I dont think anybody codes c++ all in a single file either :wink: