Re: namespace naming guidelines
sure@europa.com wrote:
In C++ Coding Standards by Sutter and Alexandrescu, there are two
guidelines dedicated to namespace scoping:
57. Keep a type and its nonmember function interface in the same
namespace.
Keep things together that belong together.
58. Keep types and functions in separate namespaces unless they're
specifically intended to work together.
Keep things separate that don't belong together.
Both guidelines simply say that you should divide things into groups in a
sensible way. Now, what this sensible way means strongly depends on the
context. As a rule of thumb, if you could imagine putting things into a
library, they all belong together and deserve a common namespace.
If I follow this advice I'll be creating lots more namespaces.
Although the book implies I should have a lot of namespaces, it
doesn't offer suggestions for regular ways to name them.
Start creating some more namespaces. At the point where the amount of
namespaces (and nested namespaces) drowns the normal code, you know that
you have gone too far.
I've also received this guidance:
Don't use the same name for a namespace and a type contained by
that namespace.
So giving the namespace the name of the most important class in
the namespace is not a great choice.
Hmmm, often this is not a problem. Let's take an example, we are refactoring
this code:
class TextConverter;
class TextToken;
....which will probably become this:
namespace TextConversion
{
class Converter;
class Token;
}
As you see, the active name "converter" of the former class name has been
transformed to the neutral form "conversion" in the namespace name. This
automatically avoids the temptation.
One more note here: the name "TextConversion" might have been shortened to
just "Text" - that way the names would have been closest to the former
names. However, the class Token only makes sense in combination with this
special conversion of texts and not texts in general. Just the same, this
could all have been in a namespace called Conversion which is nested in
another namespace named Text, but this would have been too fine-grained for
my task. It might have made sense in a different context though. Just apply
common sense and experience. ;)
I'm looking for wisdom on what to name my soon-to-be blossoming
namespaces. I'm hoping for something as formulaic as naming
private member variables with a trailing underscore.
I don't think there is such an easy rule. After all, the trailing underscore
is just a simple flag telling you something is a member. This is like basic
grammar compared to poetic value[1] of a text.
Uli
[1] I'm reluctant to apply the term value here, but can't find a better
word... :/
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]