Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the guidelines for avoiding namespace and type name conflicts in C#? [closed]

Tags:

namespaces

c#

Clean code should use short, meaningful names for entities. Thus, given an application that deals with, say, robots, I would want to have a class library project Robot with namespace Robot that has a class Robot. Lets assume that there is only a single robot object, like e.g. the code of Hubot.

However, naming a class name the same as its namespace is an extremely bad idea in C#.

What are the guidelines for avoiding namespace and type name conflicts?

Specifically, considering the case above, how should I name the namespace if I want to keep the class name Robot?

like image 873
mrts Avatar asked Sep 13 '13 18:09

mrts


People also ask

How do namespaces prevent the problem of name collisions?

There are several techniques for avoiding name collisions, including the use of: namespaces - to qualify each name within a separate name group, so that the totally qualified names differ from each other. renaming - to change the name of one item (typically the one used less often) into some other name.

What are the two types of namespaces?

When creating a namespace, you must choose one of two namespace types: a stand-alone namespace or a domain-based namespace. In addition, if you choose a domain-based namespace, you must choose a namespace mode: Windows 2000 Server mode or Windows Server 2008 mode.

What is the difference between name and namespace?

Names are what can be traditionally thought of as "variables". Namespaces are the places where names live.


3 Answers

The guidelines are very clear: namespaces outside of System should be Company.Technology. This allows both clear disambiguation and makes it easier for users to discover what namespaces are associated with what technologies. Remember, the primary purpose of a namespace is not collision avoidance, but rather developer productivity.

Guidelines are here:

http://msdn.microsoft.com/en-us/library/893ke618(v=vs.71).aspx

Your namespace should be something like:

namespace MrtsCorp.Robotics 
{
  public sealed class Robot 
  {
     ...

If you want to look at a reasonable model for such a namespace, try these:

http://msdn.microsoft.com/en-us/library/dd159952.aspx

I am not thrilled with namespaces with names like Ccr, which are clear only to domain experts, but Microsoft.Robotics.Simulation is nicely descriptive.

like image 170
Eric Lippert Avatar answered Sep 28 '22 09:09

Eric Lippert


The easiest workaround, if you really can't come up with anything, is to call the namespace Robots.

like image 29
Wiktor Zychla Avatar answered Sep 28 '22 08:09

Wiktor Zychla


From the Framework Design Guidelines on Names of Namespaces a namespace should be in the following format:

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

so since the "company" here is the open source project team for Hubot and really none of the other categories apply here then for your example it would be something like:

namespace HubotDev.Hubot
{
    public sealed class Robot
    {
       //...
    }
}

And the useage would be

Hubot.Robot robot = //...;
like image 35
Scott Chamberlain Avatar answered Sep 28 '22 09:09

Scott Chamberlain