views:

339

answers:

7

I'm writing a C++ program that has a large number of classes. In my head I can visualise them as distinct collections. For example there's a collection of classes for reading and storing config data, and another collection for drawing a user interface with various widgets.

Each of those collections could be neatly stored inside separate namespaces, which seems sensible. The config part has a "screen" class, and the GUI part also has a "screen" class, but they are different to each other. I could rename one "gui_screen" and the other "config_screen", but that's the point of a namespace isn't it? To remove these prefixes we invent to separate things.

Part of me then thinks it'd be tidy to store those namespaces inside one main one so that none of my code can interfere with the namespaces of anything else. And I suppose it might also make the code more readable too.

Or am I just making overly complex hierarchies of data for no reason at all?

A: 

Defining namespaces you are not doing any hierarchy, so I don't see what the problem is, anyway namespace are there just to solve your very problem.

Gaetano Mendola
+6  A: 

If you're having namespaces with common names such as "gui" and "config", and it's possible that your code may in the future be part of a library meant to be linked with other code not under your control, it might indeed be a good idea to nest all of "your" namespaces into a single named one (which probably should have no other content except the nested namespaces) which identifies them as "yours". There is really no penalty for doing so, especially if, as you think, it can be done in a way that helps readability. (That's quite different from deep nested hierarchies of class inheritance, which can definitely hamper clarity, as different functionality is added at each layer and a reader or maintainer of the code has to be jumping around among many files to see or change what's added where!-)

Alex Martelli
A: 

I personally love to group files/classes by their functionality. I normally start with a folder with a name that matches the namespace I will use. Eventually if a namespace needs to be reused in another application, you can easily take it out and build it into it's own dll and share it.

It also makes for nice intellisense so that when I'm drilling down through namespaces, I can see just the classes that are part of that group of functionality.

Sam
+3  A: 

No, IMO you're not overdoing at all.

And resist the temptation to use using declarations or (heaven forbid!) using directives! It takes very little getting used to always typing all namespaces and, once you're used to it, it makes the code much easier to read and understand. ("Which screen type is this again, gui or config?")

sbi
+1: try to avoid `using` in any case... it just causes unnecessary grief in the end.
D.Shawley
+2  A: 

It is not overkill. It is reasonable to nest your namespaces, e.g. piku::gui::screen etc.

You could also collapse them to piku_gui_screen, but with the separate, nested namespaces you get the advantage that, if you're inside piku::gui, you can access all names in that namespace easily (e.g. screen will automatically resolve to piku::gui::screen).

It doesn matter, since with namespaces, inside `piku::gui` you can just says `screen`. You cannot skip ordinary prefixes.
sbi
@sbi: Yes, that's exactly my point.
Excellent. This is like the com.microsoft.foo stuff in .NET and similar in Java, isn't it.
Piku
@piku: Yes, it's like those Java packages.
It is like that, although one difference is that Java packages aren't hierarchical, except for the purposes of carving up the namespace and the filesystem. There is nowhere in the "com" package where a class "com.micosoft.Foo" can be referred to as "microsoft.Foo" (unless they've changed it since I learned Java). C++ namespaces are hierarchical, and if you're in or using namespace piku then `gui::screen` finds `piku::gui::screen`. It's probably rare for this difference to actually matter, but there it is.
Steve Jessop
+3  A: 

Personally I've never seen more than two levels of namespace nesting that was at all meaningful or useful. What you've described sounds fine, but I wouldn't go any deeper than project::component unless you've got a tangible, demonstrable, "this breaks without it" reason to do so. In other words, foo::bar::screen is reasonable, foo::bar::ui::screen is highly questionable, and anything more than that almost certainly introduces more complexity than is justified.

Darryl
A: 

It seems to me that one namespace per developer ought to be enough -- you (presumably) have control over all the type names you define in your own code, and it's unlikely that you'll want to use the same type name for different types within the same project.

I think the main use of namespaces is to handle the case where two independent developers happened to choose the same type name -- without namespaces you'd have no way to use both developers' code in the same project. Multiple namespaces within a single developer's codebase, OTOH, just add complexity and typing overhead, with little or no compensating benefit.

All IMHO, of course.

Jeremy Friesner