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

Best practices for referring to other namespaces in header files

[ This is a slightly modified re-post from c.l.c++.m ]

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 {};

namespace module1 {

class SomeOtherClass {};

} // module1

namespace module2 {

...

} // module2

} // lib

} // company

In a header file, from inside company::lib::module2, what would be the
preferred way to refer to:

1. std::vector
2. company::lib::SomeClass
3. company::lib::module1::SomeOtherClass

I've got a few alternatives myself:

1(a) '::std::vector'
1(b) 'std::vector'

2(a) '::company::lib::SomeClass'
2(b) 'company::lib::SomeClass'
2(c) 'lib::SomeClass'
2(d) 'SomeClass'

3(a) '::company::lib::module1::SomeOtherClass'
3(b) 'company::lib::module1::SomeOtherClass'
3(c) 'lib::module1::SomeOtherClass'
3(d) 'module1::SomeOtherClass'

Each one of the the alternatives has its pros and cons in terms of
readability and stability (not being affected by someone e.g.
introducing company::lib::module2::SomeClass in combination with
alternative 2(d) above).

What do you do in practice (assuming you do use nested namespaces)? I'd
especially appreciate comments backed by some real-world experience
maintaining libraries/applications.

// Johan

Jul 23 '05 #1
3 1683
"Johan Nilsson" <ju********@gmail.com> wrote in message
In a header file, from inside company::lib::module2, what would be the
preferred way to refer to:

1. std::vector
2. company::lib::SomeClass
3. company::lib::module1::SomeOtherClass

I've got a few alternatives myself:

1(a) '::std::vector'
1(b) 'std::vector'
(1b) seems most standard, as std::vector is so common.
2(a) '::company::lib::SomeClass'
2(b) 'company::lib::SomeClass'
2(c) 'lib::SomeClass'
2(d) 'SomeClass'

3(a) '::company::lib::module1::SomeOtherClass'
3(b) 'company::lib::module1::SomeOtherClass'
3(c) 'lib::module1::SomeOtherClass'
3(d) 'module1::SomeOtherClass'
(2d), (3d), the minimal solutions seem best to me.
Each one of the the alternatives has its pros and cons in terms of
readability and stability (not being affected by someone e.g.
introducing company::lib::module2::SomeClass in combination with
alternative 2(d) above).
Stability is not an issue, as there will likely be compile errors.
Readibility is best. Besides, it seems best design that only namespaces at
the same level use the same names -- so if there is a
company::lib::module2::SomeClass then there can be a
company::lib::module1::SomeClass, but there probably should not be a
company::lib::SomeClass as this opens up the stability issue you just
mentioned.
What do you do in practice (assuming you do use nested namespaces)? I'd
especially appreciate comments backed by some real-world experience
maintaining libraries/applications.

Jul 23 '05 #2
Siemel Naran wrote:
"Johan Nilsson" <ju********@gmail.com> wrote in message

[snip]
2(a) '::company::lib::SomeClass'
2(b) 'company::lib::SomeClass'
2(c) 'lib::SomeClass'
2(d) 'SomeClass'

3(a) '::company::lib::module1::SomeOtherClass'
3(b) 'company::lib::module1::SomeOtherClass'
3(c) 'lib::module1::SomeOtherClass'
3(d) 'module1::SomeOtherClass'
(2d), (3d), the minimal solutions seem best to me.
Each one of the the alternatives has its pros and cons in terms of
readability and stability (not being affected by someone e.g.
introducing company::lib::module2::SomeClass in combination with
alternative 2(d) above).


Stability is not an issue, as there will likely be compile errors.
Readibility is best. Besides, it seems best design that only

namespaces at the same level use the same names -- so if there is a
company::lib::module2::SomeClass then there can be a
company::lib::module1::SomeClass, but there probably should not be a
company::lib::SomeClass as this opens up the stability issue you just
mentioned.


That was actually what I meant with stability - protect oneself (and
users) against such future changes. OTOH, there's the readability
issue. Not many people seem to care, or they simply don't use
namespaces that much, so I guess I'll have to use common sense and just
make up my mind.

Currently leaning towards always using fully qualified names, without
the global namespace prefix.

// Johan

Jul 23 '05 #3
"Johan Nilsson" <ju********@gmail.com> wrote in message
Siemel Naran wrote:

Stability is not an issue, as there will likely be compile errors.
Readibility is best. Besides, it seems best design that only

namespaces at
the same level use the same names -- so if there is a
company::lib::module2::SomeClass then there can be a
company::lib::module1::SomeClass, but there probably should not be a
company::lib::SomeClass as this opens up the stability issue you just
mentioned.


That was actually what I meant with stability - protect oneself (and
users) against such future changes. OTOH, there's the readability
issue. Not many people seem to care, or they simply don't use
namespaces that much, so I guess I'll have to use common sense and just
make up my mind.

Currently leaning towards always using fully qualified names, without
the global namespace prefix.


It might be hard to enforce, because if one of the programmers forgets the
prefix (which could happen in a large project), the code will compile and
we'll never know there was a coding standards violation. Also, I imagine
the violations would be difficult to catch in a code review. One could
write a tool to catch such coding violations though.

One may somehow use namespace aliases (to typedef one namespace to another)
to simplify things, but I've never done it before, so don't know firsthand.
Jul 23 '05 #4

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

Similar topics

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...
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...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
2
by: steve | last post by:
Can someone point me to some information on namespace best practices? Questions I have are as follows: 1) Should I use .NET namespaces or URL, URI and URN's? Ie mycompany.mydivision.myapp vs...
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"...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
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...
6
by: Daniel Kraft | last post by:
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 { ... }...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.