From the Namespace Naming Guidelines:
The general rule for naming namespaces
is to use the company name followed by
the technology name and optionally the
feature and design as follows.
CompanyName.TechnologyName[.Feature][.Design]
Generally, it's a really bad practice to start including things into the default namespace of a framework or library. This can cause confusion in terms of whether a new namespace is part of the existing library that is part of a framework that is distributed to everyone or is part of a custom framework that was added by someone else.
Also, the naming convention tries to avoid namespace collisions by having unique identifiers such as CompanyName
. It also reduces any confusion and issues in terms of the source of the new library.
This is not only a Microsoft thing but also in Java. Namespaces in Java, called "packages" have the following convention:
The prefix of a unique package name is
always written in all-lowercase ASCII
letters and should be one of the
top-level domain names, currently com,
edu, gov, mil, net, org, or one of the
English two-letter codes identifying
countries as specified in ISO Standard
3166, 1981.
Subsequent components of the package
name vary according to an
organization's own internal naming
conventions. Such conventions might
specify that certain directory name
components be division, department,
project, machine, or login names.
So, if I had a super awesome piece of software, it may be in the net.coobird.superawesomesoftware
package.
And using package names that contain the default java.
, javax.
, com.sun.
packages are a big no-no.
Typically namespaces are pluralized, so as not to collide with class names (e.g. it is likely you would want classes named Vehicle
and Car
) so I'd be inclined to use namespaces as follows:
namespace Vehicles;
namespace Vehicles.Cars;
namespace Vehicles.Trucks;
As for the names of classes, it would be typical to prefix the class name with the specialization, especially if they are likely to be used together, so you'd end up with something like:
class CarWheel : Wheel
class TruckWheel : Wheel
You can see this type of 'redundancy' everywhere in the .NET Framework, for example in the System.Xml
namespace virtually all classes are prefixed with Xml
, or in the System.Data.SqlClient
namespace most classes are prefixed with Sql
. It means that you can import namespaces with the using
directive and then not have to fully-qualify class names throughout your code, e.g. which of the following is more readable?
Vehicles.Cars.Wheel wheel = new Vehicles.Cars.Wheel();
or
CarWheel wheel = new CarWheel();
It's obvious what both are doing, but the second is considerably shorter.
Note that if you do include the specialization in the name, then you may find that you don't need all the nested namespaces (.Cars
, .Trucks
, etc.) which can become painful if they are usually used together, and so every file using them would have to import all the namespaces, e.g.
using Vehicles;
using Vehicles.Cars;
using Vehicles.Trucks;
using Vehicles.SomethingElse;
using Vehicles.YetAnotherThing;
If you find this same stack of using
directives is at the top of each file, then collapse the classes down into a single namespace. You typically include all related functionality that is expected to be used together in a single namespace, and only use nested ones for functionality that extends the base namespace but is less frequently used.
Best Answer
Personally for me it"depends". Usually I would prefix everything with the word
Character
to keep things consistant, however if you have everything already under theCharacter
namespace the Character prefix could seem redundant.I could easily see going with the shorter convention of
Models.Character.[X]
if there never will be another class calledDetails
, if there for instance could beUserDetails
thenDetails
andUserDetails
could be confusing when looking back at the code weeks or months from now and I would personally prefer then theCharacterDetails
option.In the end it is your personal preference, what more accurately describes your domain,
Details
orCharacterDetails
?