27

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++?

0

4 Answers 4

50

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 than 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 classes 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.

Sign up to request clarification or add additional context in comments.

11 Comments

Ok, if I'll add then domainObject::Settings and other guy will add DomainObjectValidator and so on - there will be a mash.
Why will you be including some other product's include files into your product?
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.
Putting product name is the base Code convention. Like putting ProductPrefix to all classes and functions.
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.
|
5

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.

2 Comments

So there is no different "Domain Areas"? I mean all business classes are in the same namespace?
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.
2

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.

2 Comments

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.
Boost uses them to prevent name clashges, like I said.
0

I divide namespaces depending on its usages:

I have a separate namespace, where I have defined all my interfaces (pure virtual classes).

I have a separate namespace, where I have defined my library classes (like db library, processing library).

And I have a separate namespace, where I have my core business (business logic) objects (like purchase_order, etc).

I guess, its about defining it in a way, that doesn't becomes difficult to handle in the future. So, you can check the difficulties that will surround on your current design.

And if you think they are fine, you should go with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.