Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace Rule of Thumb [closed]

Is there a general rule of thumb as to how many classes, interfaces etc should go in to a given name space before the items should be further classfied in to a new name space? Like a best practice or a community preference? Or is this all personal preference?

namespace: MyExample.Namespace
 interface1
 interface2
 interface3
 interface4
 interface5
 interface6
 interface7
 interface8
 interface9

Or

namespace: MyExample.Namespace.Group1
 interface1
 interface2
 interface3
namespace: MyExample.Namespace.Group2
 interface4
 interface5
 interface6
namespace: MyExample.Namespace.Group3
 interface7
 interface8
 interface9
like image 458
Frank V Avatar asked Jan 10 '09 22:01

Frank V


People also ask

What do you mean by namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

How do you name a namespace in C++?

Namespaces should have unique names based on the project name, and possibly its path. Do not use using-directives (e.g., using namespace foo ). Do not use inline namespaces. For unnamed namespaces, see Internal Linkage.


2 Answers

I have not seen any rule of thumb at any reliable source but there are a few common preferences that I haven seen while working with most developers. There are a few things that help you make the namespaces.

  1. Domain of the class
  2. Is it a class or an interface (I have seen some developers prefer namespaces like ShopApp.Model.Interfaces ). Works really well if your interfaces are some service or data contract.
  3. Dont have namespaces that are too deep, 3 (.) is enough. More than that may get annoying.
  4. Be open to reorganize namespace if at anytime u feel it has become illogical or hard to manage.
  5. Do not create namespaces just for the sake of it.
like image 184
Perpetualcoder Avatar answered Nov 09 '22 19:11

Perpetualcoder


If building a library or a module, it is generally better to use only one namespace, since the primary function of a namespace is to avoid name collisions and you have the control over what names get assigned to classes and interfaces.

like image 21
Alex B Avatar answered Nov 09 '22 19:11

Alex B