473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

More or less philosophical thoughts about 'using namespace'

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.
Mar 31 '07 #1
6 2231
"Juha Nieminen" writes:
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!
Mar 31 '07 #2
On Mar 31, 12:06 pm, Juha Nieminen <nos...@thanks.invalidwrote:
...

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-lit...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
}

Mar 31 '07 #3
Salt_Peter wrote:
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).
Apr 1 '07 #4
osmium wrote:
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.
Apr 1 '07 #5
On Mar 31, 6:06 pm, Juha Nieminen <nos...@thanks.invalidwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 1 '07 #6
On Apr 1, 7:12 am, Juha Nieminen <nos...@thanks.invalidwrote:
Salt_Peter wrote:
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).

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.

Apr 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
4
by: Madhav | last post by:
Hi all, I am a newbie in c++. I want to know what is the philosophical reason behind the existence of friend functions. I thought giving access to private data to a function which is not a member...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
5
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
1
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.