473,791 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2253
"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::strin g" 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::strin g" 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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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::strin g" 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::strin g" 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
17787
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
11
2605
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
39
2815
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 incomparable, they are not equal but neither is one less or greater than the other. So I thought that either cmp could return None in this case or throw a specific exception. People writing a __cmp__ method could do the same.
4
2363
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 of the class is a violation of encapsulation. Thanks, Madhav.
11
8797
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
7
7818
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}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
5
1802
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
1732
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 JSON extension that allows cross-references, and I'm asking for your thoughts on a simple type system. I've dubbed this variation RJSON (Red's JSON) for lack of a shorter name. Here's how it works right now:
14
17202
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
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.