473,772 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best practice for 'using namespaces'

Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulat e" 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::par t1;

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 5254
"Daniel Kraft" <d@domob.euwrot e in message
news:f5******** **@newsreader2. utanet.at...
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulat e" 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::par t1;

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.euw rote:
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulat e" 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::par t1;

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::par t1;

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.euwrot e:
Hi all,

in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulat e" 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::par t1;
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.dyn dns.org R'lyeh wgah'nagl fhtagn!
Jun 24 '07 #5
On Jun 24, 10:09 pm, Jorgen Grahn <grahn+n...@sni pabacken.dyndns .org>
wrote:
On Sat, 23 Jun 2007 10:13:14 +0000, Daniel Kraft <d...@domob.euw rote:
Hi all,
in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulat e" 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::par t1;
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.dyn dns.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
273
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 Namespaces. I read somewhere that each directory should have it's own namespace, but that doesn't seem right to me.... thanks -- /dave
3
1720
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 lib { class SomeClass {};
0
1523
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 could be a dozen in each, and there may be a thumbnail/full-size pair too. Maybe even video clips. Size is _not_ an issue here, as the feed is intended for transferring content to partners who really do want it all, not just casual readers looking...
136
9456
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
1
2073
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 my instance, to declare the namespace, I have this on my root element: <root xmlns:xlink="http://www.w3.org/1999/xlink"> ... <mylink xlink:href="http://foo.com"> ....
3
2444
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 project classes with 'using' meaning
1
2279
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 Struction my solution in numerous projects ie 1. Webpage Files(ui) 2. Classes
14
6704
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" process I am introducing namespaces to properly compartmentalise sections of the code into logical units. What I am speciffically trying to get right is the dependency tree for header files to reduce compile time and simplify the code structure. On...
3
4037
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 classes are obviously related, then I use the same namespace. Problem is that this doesn't seem to happen often. I could understand Balena's numbers in the context of a specific library, but in the scope of a large program, many corners are...
0
9454
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
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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
10038
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
9912
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...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2850
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.