tags:

views:

1299

answers:

3

On the project we are trying to reach an agreement on the namespace usage. We decided that the first level will be "productName" and the second is "moduleName".

productName::moduleName

Now if the module is kind of utility module there is no problem to add third namespace. For example to add "str": productName::utilityModuleName::str - to divide space where all "strings" related stuff will go.

If the module is the main business module we have many opportunities and almost no agreement.

For example

class productName::mainModuleName::DomainObject

and

class productName::mainModuleName::DomainObjectSomethingElseViewForExample

can be both at

namespace productName::mainModuleName::domainObject
class Data
class ViewForExample

Why should we create inner not private classes and not namespaces? Why should we create class where all methods are static (except cases when this class is going to be template parameter)?

Project consist of 1Gb of source code. So, what is the best practice to divide modules on namespaces in the c++?

+2  A: 

This is all subjective, but I would hesitate to go more than 3 levels deep. It just gets too unwieldy at some point. So unless your code base is very, very large, I would keep it pretty shallow.

We divide our code into subsystems, and have a namespace for each subsystem. Utility things would go into their own namespace if indeed they are reusable across subsystems.

Brian Neal
So there is no different "Domain Areas"? I mean all business classes are in the same namespace?
Mykola Golubyev
Well it's hard to understand what you mean without seeing your architecture, but a namespace per Domain Area sounds like a good idea to me. The whole idea is to prevent name clashes when mixing different areas of the code.
Brian Neal
+10  A: 

What namespaces are for:

Namespaces are meant to establish context only so you don't have naming confilcts.

General rules:

Specifying too much context is not needed and will cause more inconvenience then it is worth.

So you want to use your best judgment, but still follow these 2 rules:

  • Don't be too general when using namespaces
  • Don't be too specific when using namespaces

I would not be so strict about how to use namespace names, and to simply use namespaces based on a related group of code.

Why namespaces that are too general are not helpful:

The problem with dividing the namespace starting with the product name, is that you will often have a component of code, or some base library that is common to multiple products.

You also will not be using Product2 namespaces inside Product1, so explicitly specifying it is pointless. If you were including Product2's files inside Product1, then is this naming conversion still useful?

Why namespaces that are too specific are not helpful:

When you have namespaces that are too specific, the line between these distinct namespaces start to blur. You start using the namespaces inside each other back and forth. At this time it's better to generalize the common code together under the same namespace.

Classes with all static vs templates:

"Why should we create inner not private classes and not namespaces? Why should we create class where all methods are static"

Some differences:

  • Namespaces can be implied by using the using keyword
  • Namespaces can be aliased, classes are types and can be typedef'ed
  • Namespaces can be added to, you can add functionality to it at any time and add to it directly
  • Classes cannot be added to without making a new derived class.
  • Namespaces can have forward declarations.
  • With classes you can have private members and protected members
  • Classes can be used with templates

Exactly how to divide:

"Project consist of 1Gb of source code. So, what is the best practice to divide modules on namespaces in the c++?"

It's too subjective to say exactly how to divide your code without the exact source code. Dividing based on the modules though sounds logical, just not the whole product.

Brian R. Bondy
Ok, if I'll add then domainObject::Settings and other guy will add DomainObjectValidator and so on - there will be a mash.
Mykola Golubyev
Why will you be including some other product's include files into your product?
Brian R. Bondy
The fact that you may run into this problem means that you shouldn't be marking them with a product name anyway, because that means you are using this module in a product where its namespace doesn't fit.
Brian R. Bondy
Putting product name is the base Code convention. Like putting ProductPrefix to all classes and functions.
Mykola Golubyev
I would hate to work on that code if there was a product name change. It is better to name things based on what the code does.
Brian R. Bondy
Putting product prefix to all classes and functions is even worse.
Brian R. Bondy
It is an old code which meat c++ while it already was working and selling.
Mykola Golubyev
I would never say to waste time changing old code, I prefer to be more relaxed on coding standards for situations like this and fix over time. If nothing else you can distinguish old code from new code.
Brian R. Bondy
It is the place where the problem began. We support an old code and meantime implement new functionality in a c++ way. Every body in the project use namespace one way or another.
Mykola Golubyev
I don't think there will be a problem as long as you are following the above guidelines.
Brian R. Bondy
I've add two "Why" to the question at the end. Could you be so kind to answer those too, please.
Mykola Golubyev
ok added responses to those too
Brian R. Bondy
+3  A: 

It seems to me that you are trying to use namespaces as a design tool. They are not intended for that, they are intended to prevent name clashes. If you don't have the clashes, you don't need the namespaces.

anon
Is that so? why then boost introduce so many different levels? It could just use long names. like boostThreadJoinAll.Namespace is tool to browse as well. Kind of show me all related to the Domain.
Mykola Golubyev
Boost uses them to prevent name clashges, like I said.
anon