60

For those of you out there writing reusable components, what do you consider to be best practice if you're extending the functionality of the .NET framework?

For example, I'm creating a Pop3 library at the moment as one doesn't exist in .NET. Do I create a custom namespace or do I use System.Net.Mail ?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Razor
  • 17,271
  • 25
  • 91
  • 138

2 Answers2

76

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.

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
coobird
  • 159,216
  • 35
  • 211
  • 226
  • 2
    @Sir Psycho: Coobird is right but you also need to keep an open mind set you dont want a namespace that ends up being too deep. I start getting worried after I type 3 '.' in a namespace name. – Perpetualcoder May 28 '09 at 02:47
  • @Sir Psycho: The namespace guidelines also says that an well-established brand is another option instead of a company name -- these are guidelines to avoid namespace collisions. So, perhaps some title (like your username?) that is unique from others may be a good substitute for CompanyName :) – coobird May 28 '09 at 03:21
  • @LILkilaaBEE: Alias is good when you are consuming these namespaces. Doesnt make the namespace concise in itself. – Perpetualcoder Sep 13 '11 at 14:57
  • 1
    Used this answer as my guidance then hit an issue not covered, do not name a class the same as its namespace. It causes reference problems but don't take my word for it, read the full details here: http://blogs.msdn.com/b/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx – RyanfaeScotland Sep 19 '14 at 10:05
  • 1
    Is there an example that includes the [.Design] part of the namespace? – Kevin Holt Aug 03 '16 at 03:01
  • .NET 4 version of 1st link above https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms229026(v%3dvs.100) – dthal Mar 28 '19 at 21:49
19

Also have a look at following MSDN article for guidelines about naming Namespaces

Names of Namespaces

The name chosen for a namespace should indicate the functionality made available by types in the namespace. For example, the System.Net.Sockets namespace contains types that enable developers to use sockets to communicate over networks.

The general format for a namespace name is as follows:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

For example, Microsoft.WindowsMobile.DirectX.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • 2
    What is a full example using all parts? Microsoft.WindowsMobile.DirectX.Lines? – O.O Dec 02 '14 at 21:56