0

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?

jkeys
  • 3,803
  • 11
  • 39
  • 63
  • 3
    Your question implicitly assumes that `using namespace std;` is always bad. Personally, I think there's a fair share of small programs (and note that most examples are short programs) that for them, it doesn't really matter. Removing `std::` will make the code shorter and keeps the focus to the more important ideas being expressed by the specific code listing. – Mehrdad Afshari Jul 26 '09 at 19:21
  • 4
    I withhold consent from that consensus, and I rarely do 'using namespace std', in SO example code or elsewhere :-) – Steve Jessop Jul 26 '09 at 22:34
  • 3
    same as onebyone. I understand why a lot of people find it harmless to do in .cpp files, but personally I generally just type out the namespace prefix. It's so short, it doesn't really bother me. I generally do the same in SO examples to avoid ambiguity and make it clear which classes and functions belong to the standard library. – jalf Jul 26 '09 at 22:36
  • I do sometimes use using declarations to save typing std::, but only with the names I plan on using a lot. Anything involving is the most likely to make me give up and use a using directive, since you tend to need 15 different classes and the lines are too long at the best of times. Maybe I'm doing it wrong... – Steve Jessop Jul 26 '09 at 23:08
  • I think most _questions_ on SO use `using namespace std;`, but the consensus among answers is _not_ to do that. – Mooing Duck Aug 26 '13 at 17:32

6 Answers6

7

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
  • 243,077
  • 51
  • 345
  • 550
  • 2
    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 Jul 26 '09 at 21:12
  • 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 Jul 26 '09 at 22:38
  • 1
    I personally find it more natural to read things with a `std` prefix. If I see an unqualified `string`, it looks weird to me. – GManNickG Jul 27 '09 at 08:00
  • 1
    @GMan: I agree. But we're not beginners, so I'm not sure our opinions count. ;) – jalf Jul 27 '09 at 13:47
3

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
  • 5,496
  • 3
  • 25
  • 29
2

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
  • 114,398
  • 98
  • 311
  • 431
  • 2
    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 Jul 26 '09 at 23:02
  • 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 Jul 26 '09 at 23:12
  • 1
    @ThomasOwens: On the other hand, saying the name of the text type is `std::string`, and then explaining why it contains `std::` later is only a hair more tricky, and leads to far better programming practices in the future, instead of asking them to suddenly change the way they write code. – Mooing Duck Aug 26 '13 at 17:36
2

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
0

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
  • 44,864
  • 6
  • 88
  • 112
0

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
  • 19,435
  • 18
  • 63
  • 87