More or less philosophical thoughts about 'using namespace'

O

osmium

Juha Nieminen said:
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 noise think noise you noise are noise onto noise something noise here.

Keep noise spreading noise the noise faith!
 
J

Juha Nieminen

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.
 
S

Salt_Peter

...

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.

You sound more like a cult than a coder.
How many more paragraphs do you need to cover an issue addressed
weekly here (if not daily)?
You also forgot to mention when and why "using namespace std;" is not
a good solution.

http://www.parashift.com/c++-faq-lite/coding-standards.html
[27.5] Should I use using namespace std in my code?

Some alternatives work just as well:

#include <string>

int main()
{
using std::string;
string s;
// do stuff
}
 
J

James Kanze

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.

You're kidding, right. I see it in beginners' code, but that's
about it. All of the coding guidelines I've used forbid it.

[...]
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.

Exactly. You write code once, you read it many times. And it
is very laborious reading code which doesn't specify where the
symbols come from. The name of the type is std::string, not
string.
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'll sometimes make an exception for my test suites, e.g.: when
testing Gabi::Toto, I might use a "using namespace Gabi" in the
test code itself. But that's the limit.
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.

Exactly.

[...]
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.

You don't have to ask. It's a common rule in the coding
guidelines of any serious company.
 
J

Juha Nieminen

Salt_Peter said:
You sound more like a cult than a coder.

Your answer was not very constructive.
Some alternatives work just as well:

I disagree. I see absolutely no reason to write something like
"using std::string;" instead of writing "std::string" whenever it's
used. Your alternative might make the code just slightly less
unclear, but not much.

Do you have a specific reason to not to write "std::string" each
time? Because I can't think of any (other than laziness, of course).
 
J

Juha Nieminen

osmium said:
I noise think noise you noise are noise onto noise something noise here.

Keep noise spreading noise the noise faith!

Your sarcasm is invalid. If repetition is what you are worried
about, then why don't you complain about all those ints and fors and
ifs and {'s and ('s... There may be hundreds of them even in small
programs.

Do you see my point? When you see "for" in a piece of code, it says
something informative to you. It doesn't matter if that "for" appears
twenty times in the code, it's always as helpful.

Why would namespace prefixes be any different? They contain
information which helps the person reading the code. They are telling
something.

As I said, you only see noise because you want to see noise.
If you approach the subject with a different attitude you may well
start seeing them as something helpful.
 
S

Salt_Peter

Your answer was not very constructive.


I disagree. I see absolutely no reason to write something like
"using std::string;" instead of writing "std::string" whenever it's
used. Your alternative might make the code just slightly less
unclear, but not much.

Do you have a specific reason to not to write "std::string" each
time? Because I can't think of any (other than laziness, of course).


I always use std::string, but what my preferences might be are
irrelevent.
Its helpfull to present the other alternative as well if you plan to
write a lengthy article about it. You threw a long Post without
stating "why" and "when" using namespace std is not recommended.
You might consider that to be a poor response - i don't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top