473,395 Members | 1,468 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,395 software developers and data experts.

Best practice for 'using namespaces'

Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:

namespace project
{
namespace part1 { ... }
namespace part2 { ... }
}

And I want to use this somewhere else, there seem to be four possibilities:

1) using namespace project; using namespace part1; using namespace part2;

2) using project::part1::Class1; using project::part2::Class2;

3) namespace p1=project::part1;

4) prepend everything with project::part2::

When I'm writing a cpp-file, I usually find myself using 1) -- I think
this is not really good, but as I'm affecting only my own code therein,
it shouldn't be too bad.

But what to do for a header-file which might be included by someone
else? 1) and 2) bring things into his namespace he does not want, and
even 3) introduces a namespace-shortcut he does not need.

So only 4) seems to leave everything without impact, except that I do
not really want to type project::part2:: every time and at least to my
taste this makes the code more unreadable.

What are best practices for this problem, if there are any? Or what
would you suggest to do?

Cheers,
Daniel Kraft

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 23 '07 #1
6 5228
"Daniel Kraft" <d@domob.euwrote in message
news:f5**********@newsreader2.utanet.at...
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:

namespace project
{
namespace part1 { ... }
namespace part2 { ... }
}

And I want to use this somewhere else, there seem to be four
possibilities:

1) using namespace project; using namespace part1; using namespace part2;

2) using project::part1::Class1; using project::part2::Class2;

3) namespace p1=project::part1;

4) prepend everything with project::part2::

When I'm writing a cpp-file, I usually find myself using 1) -- I think
this is not really good, but as I'm affecting only my own code therein, it
shouldn't be too bad.

But what to do for a header-file which might be included by someone else?
1) and 2) bring things into his namespace he does not want, and even 3)
introduces a namespace-shortcut he does not need.

So only 4) seems to leave everything without impact, except that I do not
really want to type project::part2:: every time and at least to my taste
this makes the code more unreadable.

What are best practices for this problem, if there are any? Or what would
you suggest to do?
In my own code I don't make heavy use of namespaces, but I do use a
namespace around my utility header (std::string trim funciton, stream
converstion, etc...) and I just used a small name for the namespace: jml
(which happen to be my initials). so I go with 4.

Now, project::this project::that does seem to be a lot of typing, maybe you
could do a 5)

#define prj project
then just have to do
prj::this prj::that ?
I would presume that most namespace names would be rather largers (such as
boost::) so a 3 letter shortcut shouldn't hurt you.
Jun 23 '07 #2
On 23 Jun, 11:13, Daniel Kraft <d...@domob.euwrote:
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:

namespace project
{
namespace part1 { ... }
namespace part2 { ... }

}

And I want to use this somewhere else, there seem to be four possibilities:

1) using namespace project; using namespace part1; using namespace part2;

2) using project::part1::Class1; using project::part2::Class2;

3) namespace p1=project::part1;

4) prepend everything with project::part2::

When I'm writing a cpp-file, I usually find myself using 1) -- I think
this is not really good, but as I'm affecting only my own code therein,
it shouldn't be too bad.

But what to do for a header-file which might be included by someone
else? 1) and 2) bring things into his namespace he does not want, and
even 3) introduces a namespace-shortcut he does not need.

So only 4) seems to leave everything without impact, except that I do
not really want to type project::part2:: every time and at least to my
taste this makes the code more unreadable.

What are best practices for this problem, if there are any? Or what
would you suggest to do?
Within your own source files, as long as you understand the costs as
well as the benefits, any of 1, 2, 3 and 4 are fine. It's your code so
it's up to you. Personally, I use all 4 at one time or another (well,
maybe not number 3 very much, but that's just a personal preference).

In a header, the best practice advice is *never* to use 1 or 2 for
exactly the reason you suggest. I've not seen 3 discussed either way
very much (probably because, while misuse of "using namespace std" is
a common issue for beginners, namespace aliases aren't) but I would be
inclined to avoid it, again for the reason you suggest. Personally I
only ever use 4 in a header, but it sounds like you might be facing
longer fully-qualified names than I am. I tend to keep namespace names
relatively short specifically to reduce the impact on readability when
I use fully qualified names.

Your concern about readability is valid. Your concern about how long
it takes to type project::part2:: every time is *not*. Code is written
once but read many, many times. Therefore, while it may be worth the
cost of introducing a namespace alias if it means that every time
someone reads the code they are able to understand it more easily and
are less likely to be confused, it is unlikely to be worth the cost
solely to save you the one-off burden of a bit of extra typing (or
copy and pasting).

Gavin Deane

Jun 23 '07 #3
>1) using namespace project; using namespace part1; using namespace part2;
>>
2) using project::part1::Class1; using project::part2::Class2;

3) namespace p1=project::part1;

4) prepend everything with project::part2::

Now, project::this project::that does seem to be a lot of typing, maybe you
could do a 5)

#define prj project
then just have to do
prj::this prj::that ?
I would presume that most namespace names would be rather largers (such as
boost::) so a 3 letter shortcut shouldn't hurt you.
....which looks to me nearly the same as 3, isn't it? Except that I
could do a #undef at the end of my header, but I believe this would turn
things a litte upside-down.

And I think
#define prj something
would be even worse in a header than
namespace prj=something,
as this would hurt for instance any variable prj or the like someone
might use whereas the namespace alias does not, right?

Yours,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 23 '07 #4
On Sat, 23 Jun 2007 10:13:14 +0000, Daniel Kraft <d@domob.euwrote:
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:
....
1) using namespace project; using namespace part1; using namespace part2;
2) using project::part1::Class1; using project::part2::Class2;
3) namespace p1=project::part1;
4) prepend everything with project::part2::
....
So only 4) seems to leave everything without impact, except that I do
not really want to type project::part2:: every time and at least to my
taste this makes the code more unreadable.
Try 4) some time. You may (depending on what the code does and what
the interface looks like) find that you have to spell out the
namespace name much more rarely than you originally thought.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jun 24 '07 #5
On Jun 24, 10:09 pm, Jorgen Grahn <grahn+n...@snipabacken.dyndns.org>
wrote:
On Sat, 23 Jun 2007 10:13:14 +0000, Daniel Kraft <d...@domob.euwrote:
Hi all,
in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:
...
1) using namespace project; using namespace part1; using namespace part2;
2) using project::part1::Class1; using project::part2::Class2;
3) namespace p1=project::part1;
4) prepend everything with project::part2::
...
So only 4) seems to leave everything without impact, except that I do
not really want to type project::part2:: every time and at least to my
taste this makes the code more unreadable.

Try 4) some time. You may (depending on what the code does and what
the interface looks like) find that you have to spell out the
namespace name much more rarely than you originally thought.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
In 'The Design and Evolution of C++' Bjarne mentions that the using-
directives (like using namespace std;) should only be used for
transition programs. The newer programs should either use using-
declarations or explicit qualification (using scope-resolution, like
std::cout).

Jun 24 '07 #6
On 24 Jun, 23:59, comp.lang.super...@gmail.com wrote:
In 'The Design and Evolution of C++' Bjarne mentions that the using-
directives (like using namespace std;) should only be used for
transition programs. The newer programs should either use using-
declarations or explicit qualification (using scope-resolution, like
std::cout).
Please don't quote signatures. Thanks.

I don't know how Stroustrup feels about using directives these days,
but that advice is far from universally accepted any more. In "C++
Coding Standards", Sutter and Alexandrescu recommend in favour of
using directives. See this thread which refers to and quotes from the
book. And once you've done that, see the actual book :-)

http://groups.google.co.uk/group/com...a567584efd7815

Gavin Deane

Jun 25 '07 #7

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

Similar topics

1
by: Dave | last post by:
Hi, I'm building a website in ASP.NET. I will have a default.aspx file in each directory and sub-directory of the site. What is the best / accepted way of avoiding class naming conflicts by using...
3
by: Johan Nilsson | last post by:
I've seen many alternatives when it comes to referring to types defined in parent/sibling/other namespaces. Consider the following hypothetical namespace layout: namespace company { namespace...
0
by: Andy Dingley | last post by:
I'm just building my first Atom feeds (at http://gamesradar.com/rss/ps2/ should you care ) One of the requirements here is to include lots of rich media (screenshots) with each entry. There...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: johnmack | last post by:
In my XML instances I want to use the XLink namespace for attributes on certain elements. I'm having quite a hard time determining how to reflect this properly in my DTD and XML instances. In...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
1
by: Vincent V | last post by:
Hey i am just starting a new project and from the start i want to make sure my app is as Object Orientated as possible I have a couple of questions in relation to this Question 1: Should i...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
3
by: _DD | last post by:
I believe Balena's Best Practices book suggests grouping quite a few classes into each namespace. I don't remember a number, but this has me curious about how other programmers handle this. If...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.