473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using namespace considered bad?

In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?

Calum
Jul 23 '05 #1
12 3345
Calum Grant wrote:
In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?


Rule of thumb:
'using namespace whatever' in header files is bad because you pull in
the names of the namespace for all following (include) files (which
would defeat the purpose of the namespace).
'using namespace whatever' after all #include(s) in source files is ok
to "make the code much more readable".

R.C.

Jul 23 '05 #2
Calum Grant wrote:
In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?


Depends. Consider having another library "bar" that defines a class with the
same name (say "list" e.g.). Within a file, you'd always have to look
somewhere else to find which list is actually meant.
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes. namespaces basically serve two
purposes:

- organizing names into groups to make the code more understandable
- avoiding name clashes

"using namespace" tends to elide both advantages, basically making the whole
concept of namespaces useless. Therefore, I think it should actually be
"useless namespace" instead of "using namespace" ;-)

Jul 23 '05 #3
Rolf Magnus wrote:
Calum Grant wrote:

In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?

Depends. Consider having another library "bar" that defines a class with the
same name (say "list" e.g.). Within a file, you'd always have to look
somewhere else to find which list is actually meant.
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes.


Yes, it's pretty helpful that names clash - it would be far worse if the
name silently resolved to one or the other.

I just don't see the problem. If there's a clash you get an error, and
if there is no clash, there is no problem. So if you get a clash, all
you need to do is qualify the name and it all works again.
namespaces basically serve two
purposes:

- organizing names into groups to make the code more understandable
- avoiding name clashes

"using namespace" tends to elide both advantages, basically making the whole
concept of namespaces useless. Therefore, I think it should actually be
"useless namespace" instead of "using namespace" ;-)


I can only relate my own experiences - I have regularly put "using
namespace mylib;" at the top of my .cpp files with very few bad effects.
Though now I am reevaluating this choice.

Is "using foo::list;" better than "using namespace foo;" ?

Calum
Jul 23 '05 #4
Rolf Magnus wrote:
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes.


Only if you use them. <g> That is, if both foo and bar define 'list' but
you don't use that name there's no clash. If you try to use the name
'list' without a namespace qualifier it's ambiguous, so you have to
qualify it, either foo::list or bar::list.
--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5


Calum Grant wrote:
In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using.
Are there any articles that support this viewpoint? Is "using
namespace" definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code
so much clearer just to write "using namespace foo" at the top of the
source file to make the code much more readable?

Calum


I was advised against including the entire namespace in my header file.
Refer to this link:

http://groups-beta.google.com/group/...1244afdbfcf229
My issue is, I have a header file called common_header.h. Within
common_header.h I have
# include<iostream>
# include<vector>
# include<list>
# include<string>
# include<functional>
# include<algorithm>
# include<ctime>
// more
Now use common_header.h:

foo.h - # include "common_header.h"
bar.h - # include "common_header.h"
baz.h - # include "common_header.h"
// a few more
instead of:
using std::cout;
using std::endl;
using std::string;
// etc
in common_header. I might as well do 'using namespace std' in
common_header.

-----------------
Francis Glassborow replied with:

You make it impossible for anyone using that header to not use
namespace
std. What you do within your own implementation files is entirely your
choice but one of the major features of header files is to support
development being done by multiple programmers.
--- /////
In my implementation files have _no_ bearing on anyonelse. The notion
of having to qualify specific names was difficult for me to understand.
Admittidely, I'm curious to/will review 'namespace std'.

Jul 23 '05 #6
"Calum Grant" <no****@nospam.com> wrote in message
news:xo***************@newsfe6-win.ntli.net...
In older C++ computer books, you'll often see

using namespace std;

even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered
better to qualify names to make it clearer what symbol you are using. Are
there any articles that support this viewpoint? Is "using namespace"
definitively wrong, or just a matter of style?

How about when implementing a library "foo", would it not make the code so
much clearer just to write "using namespace foo" at the top of the source
file to make the code much more readable?

Calum


In a header file using "using" is a mistake. In a .cpp file I think you are
on your own. If you don't have any name clashes I don't see any serious
problem with a "using" clause.

Having said that, I rarely use "using". I think the argument that "using
namespace std;" saves typing is pretty dubious -- compared to the time it
takes to design and test good code, how much time is spent actually doing
the typing? I also don't like "using std::cout;" because it is
unmaintainable. I don't want my file to declare that I am using a specific
function if I am not, and I don't want to police this usage as the code is
refactored. (Of course the header files themselves pose a similar problem
but there is no way around that.) So I just write it out: "std::cout << x;".
Of course most people would recognize cout as being part of the standard
library, but the standard library is huge and getting even bigger, so unless
you sleep with a copy of Josuttis under your pillow every night you will
eventually encounter standard library names which are new to you. Finally,
when I am writing library code of my own, I refuse to put in long names to
avoid name conflicts such as cylib_dothis and cylib_dothat. I wrap
everything in a namespace and assume the client will consider the namespace
as part of the name: cylib::dothis and cylib::dothat.

Cy
Jul 23 '05 #7
ma******@pegasus.cc.ucf.edu wrote:
My issue is, I have a header file called common_header.h. Within
common_header.h I have
# include<iostream>
# include<vector>
# include<list>
# include<string>
# include<functional>
# include<algorithm>
# include<ctime>
// more
Now use common_header.h:

foo.h - # include "common_header.h"
bar.h - # include "common_header.h"
baz.h - # include "common_header.h"
// a few more
instead of:
using std::cout;
using std::endl;
using std::string;
// etc
in common_header. I might as well do 'using namespace std' in
common_header.


Isn't that considered bad as well? I mean having a project-level header file
and including it from everywhere? I mean baz.h might actually not need
ctime, but it still depends on it. It's very unlikely that foo.h, bar.h,
and baz.h all need exactly all the stuff that you have in common_header.h.
And it's always bad to include more than you need.

Jul 23 '05 #8
Rolf Magnus wrote:
Depends. Consider having another library "bar" that defines a class with
the same name (say "list" e.g.). Within a file, you'd always have to look
somewhere else to find which list is actually meant.
Also, you cannot do

using namespace foo;
using namespace bar;

because that would lead to name clashes. namespaces basically serve two
purposes:

- organizing names into groups to make the code more understandable
- avoiding name clashes

"using namespace" tends to elide both advantages, basically making the
whole concept of namespaces useless. Therefore, I think it should actually
be "useless namespace" instead of "using namespace" ;-)


For the most part I agree with you. I have, however, encountered a reason
for invoking a using directive (as opposed to a using declaration). If I
have a math library that has a lot of different little operand and operator
classes that I want to work automagically, then it makes sense to have
"using namespace math" in the smallest enclosing block of code where it is
applicable. I might also do that if it's clear that the using declaration
will eliminate a lot of clutter in a particular function, or other
restricted block of code.

I have even resorted to using nameless brackets to limit the scope of such
using directives. That is:

void foo(){
//lots of code ...
{
using namespace bar;
//more code ...
}

// more code.
}

Typically, I prefer the using declaration over the using directive, and
often find it better to simply qualify the name. I recently came across
some code that had a using directive for the purpose of avoiding opening
the namespace for member function declarations. That is:
// in the header file:
namespace foo {
class Bar{
void baz();
}

// in the .cpp file:

using namespace foo;

void Bar::baz(){}

That is not good style, IMO.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #9
Markus Dehmann wrote:

Isn't that considered bad as well? I mean having a project-level header
file
and including it from everywhere? I mean baz.h might actually not need
ctime, but it still depends on it. It's very unlikely that foo.h, bar.h,
and baz.h all need exactly all the stuff that you have in common_header.h.
And it's always bad to include more than you need.


That really seems to depend on the situation, and who you ask. Josuttis and
Vandervoorde suggest that putting all the standard headers into one big
include file will reduce compile time (considerable) under many
circumstances. They don't limit that suggestion to the standard headers
either. The caveat I have to that is that I have hit some very strange
situations in which syntax errors in my code cause the compiler to detect
errors in header files from the libraries I'm using, and these errors are
sensitive to the order in which the header files are included into the
translation unit. No fun to untangle!
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #10

Steven T. Hatton provided a response on where I 'stole' (Josuttis and
Vandervoorde) this approach.

Jul 23 '05 #11
On 2005-05-30, Calum Grant <no****@nospam.com> wrote:
Yes, it's pretty helpful that names clash - it would be far worse if the
name silently resolved to one or the other.

I just don't see the problem. If there's a clash you get an error, and
if there is no clash, there is no problem. So if you get a clash, all
you need to do is qualify the name and it all works again.
There's also a readability issue -- where did all those names come from ?

Is the Foo class defined in library X or library Y ? Maybe you know, but it's
less clear to someone else reading the same code.

For this reason, I prefer the more explicit (but verbose)

using namespace::name;

form, so that it is clear to someone reading the code in question which
namespace a given name came from.
I can only relate my own experiences - I have regularly put "using
namespace mylib;" at the top of my .cpp files with very few bad effects.
It potentially makes your code less readable to others, but that's a non-issue
unless someone else has to read your code.
Though now I am reevaluating this choice.

Is "using foo::list;" better than "using namespace foo;" ?


Yes.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #12

So lets see if I could summarize this. using namespace std in a header
file is _for the most part_ (you might argue almost always) bad
pratice. Solutions:

1.
| So I just write it out: "std::cout << x;".

Ok!! This implies I'll qualify the items with std:: I could live
with this.

2.
| In a .cpp file I think you are on your own. If you don't have any
name clashes
| I don't see any serious problem with a "using" clause.

No 'serious problem with a using clause', hence what I'm left with here
is:

foo.h
# include <iostream>
# include <map>
// more
foo.cpp
# include "foo.h"
using namespace std; // <-

bar.h
# include <string>
bar.cpp
# include "bar.h"
using namespace std; // <-

Preferred if I plan to include the entire namepace. Except now it's
possible that I'll endu up doing this for all translation units.

3.
| I also don't like "using std::cout;" because it is unmaintainable.

foo.h
# include <iostream>
# include <map>
// more
foo.cpp
# include "foo.h"
using std::cout;
using std::map;
// etc

bar.h
# include <string>
bar.cpp
# include "bar.h"
using std::string;

View potentially as unmaintanable. I agree.

Jul 23 '05 #13

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

Similar topics

8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
3
by: Jay Douglas | last post by:
Hello all, I've wrote a .dll in c#. The .dll needs to be used by a VB 6 application. At first, I couldn't set a reference from the VB app, but then I found the register for COM Interop in the...
0
by: Horia Tudosie | last post by:
Using Visual Studio 2003 This is to report a series of bugs regarding the FlagsAttribute and (independently) the usage of interfaces in Web applications. Let’s declare xColors type like: ...
1
by: Rico Rivera | last post by:
I have two files: FileA.vb FileB.vb FileA.vb contains the following sample code: NameSpace SomeNamespace Public Class SomeClassA Public Shared Function MethodA() As String
2
by: Jeff Brown | last post by:
Hi, I suspect that this isn't possible, but I figured I'd ask. My project has a root namespace, let's say it's "Root", that applies to almost every source file (which is why I've set it as the...
2
by: Joe Abou Jaoude | last post by:
I was reading in the .Net Book of the MS Press, and they said that the system namespace is the root of all namespaces and classes in the .Net class library. However I noticed the existence of the...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
6
by: Juha Nieminen | last post by:
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...
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
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.