views:

300

answers:

6

NOTE: I am pretty much a beginner myself. This question concentrates on C++ usage, since that is the only language I have experience with.

There seems to be a consensus on Stack Overflow to use using namespace std; in the code examples provided for C++. I originally learned it this way, and was never taught WHY this is a problem later on.

I was just wondering why people have a problem with using the prefix std:: in their example code. It seems capricious to declare a global namespace, especially since many of the questioners copy+paste the code from the example to their IDE. Namespaces are an advanced programming concept, but I think it would be best to prefix std:: and then explain later if beginners ask about it.

Why is it acceptable to teach beginners this usage?

+2  A: 

I think it simplifies it. When you are a beginner, you need to learn about things like variables, control structures, methods/functions, and in C/C++ things like pointers and references. You shouldn't have to worry about things like namespaces and packaging until after you have the basics that apply to every programming language.

Thomas Owens
I don't think you have to learn about packaging to know that there's a class called "std::string" that you'll be using a lot. Knowing that "string" and "std::string" are the same thing provided that you've done "using namespace std;", now that requires worrying about namespaces. So I think using directives complicate it. Evidently there cannot be agreement on this point :-)
Steve Jessop
Since most beginners won't be using things outside of the standard namespace for a while, I think it's OK to say "put this line here" and then explain why later. However, if you have a curious student, then explain it to them, but don't weigh down the beginners who are worrying about other things.
Thomas Owens
+5  A: 

For a beginner, IMO, it's much more important for them to understand concepts like functions, classes, conditionals and the like. Explicit namespace declarations just get in the way.

Once they understand those basic concepts, explaining the benefits of namespaces to them isn't that hard.

thedz
A: 

I disagree with the premise of your question that answering a question in the quickest way possible (or at least with the least amount of typing) is didactic in any way. You can't constantly be putting disclaimers on questions like "error checking not included" or "perhaps you don't want to use a global namespace". Bottom line is that answers to questions are not necessarily meant to be an example of the best possible way to code up a solution.

JP Alioto
+5  A: 

I think the answer is "it doesn't really matter". It's a subtlety that's fairly easy to pick up and correct later.

Every beginners' programming text I know of makes a lot of simplifications and uses a lot of handwaving to hide a lot of what's going on ("this line is magic. Just type it in, and we'll discuss what it does later").

Beginners have enough to worry about without having to fully understand everything in their code, and how/why it is bad, so often these simplifications are good things.

Although in this case I kind of agree with you.

Adding the std:: prefix wouldn't be a big deal, and it would demystify namespaces quite a bit. using namespace std is actually much harder to explain and understand properly.

On the other hand, it takes up space and adds noise to the code which should be as concise and clear as possible.

jalf
+1 I agree with you, except the last line.
AraK
+1 I agree with you especially because of the last line :)
Janusz
I don't understand how you can agree or disagree with the last line. I thought it was pretty factual. In code examples targeted at beginners, you *do* want clean, concise code, I don't see how anyone can dispute that. And adding the `std::` prefix *does* take up space and adds noise that isn't strictly necessary. Those are just facts. The subjective part is whether or not this is enough justification for just doing `using namespace std`, and I didn't conclude anything there. ;)
jalf
Although you don't conclude anything, the comment kinds of leads somewhere. Would you say that not doing `#define vec std::vector` "adds noise to the code which should be as concise and clear as possible"? I mean, it does add noise, "vector" is noiser than "vec" since there are no other classes in std called vec-anything. But calling it "noise" implies it's a bad thing and/or that it detracts from clarity ;-)
Steve Jessop
I personally find it more natural to read things with a `std` prefix. If I see an unqualified `string`, it looks weird to me.
GMan
@GMan: I agree. But we're not beginners, so I'm not sure our opinions count. ;)
jalf
A: 

It's not a pressing matter at a beginner's stage, however - they should be made aware that,

namespace A
{
int a;
}

namespace B
{
int a;
}

A::a and B::a are two different things

Maciek
+1  A: 

There is always one global namespace, you can't get rid of it, and as every other namespace is nest somewhere within it, you can't really avoid it.

The real question is what do you put in it. I don't think that it's a bad idea for beginners to put their identifiers directly in the global namespace. In fact, if you are writing the main function, then I see no reason not to use all of the global namespace as you see fit. Only once you are writing a library for others to use does it become imperative that you minimize your impact on the global namespace, usually by injecting a very small number (one?) of namespaces into it.

Whether using namespace std; is a good idea or not is a separate, but related subject.

I personally believe that beginners should never do using namespace std;. By definition, they are the sort of users who will not know every identifier that the std namespace might contain and error messages partially caused by name clashes can be very confusing.

For example, with the right (or wrong!) includes the following 'beginner style' code produces an error about count being ambiguous. An expert would have no problem fixing the code. A beginner might be stumped.

using namespace std;
int count = 3;
int main()
{
    cout << count << endl;
    return 0;
}

Also, using using namespace std; requires you to introduce the word namespace and either the concept of namespaces, or fudge the directive as 'required magic' or some such. Without it, you can just say the name of the standard string type provided by C++ in the <string> header file is std::string. The namespace concept itself only has to be understood when placing things into separate namespaces or injecting things from one namespace into another. These are both much more advanced topics that can be save until later.

Charles Bailey