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
?
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.
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.
Names are what can be traditionally thought of as "variables". Namespaces are the places where names live.
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.
The easiest workaround, if you really can't come up with anything, is to call the namespace Robots
.
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 = //...;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With