views:

139

answers:

3

One can often see class names carrying a reference to the namespace to which they belong. One of the most popular examples is the .NET 'Xml' namespace where every single class defined in it is prefixed with the 'Xml' tag.

This has always seemed pretty redundant to me, but recently I've realized that in some cases it could be useful... but still redundant.

I'd like to know how people actually deal with this issue.

Thanks.

+4  A: 

I take the "when it is appropriate" approach. The idea is that you want to name your classes using the name that makes the most sense. In some cases it does mean that the class name is somewhat redundant with the namespace, but keep in mind that the namespaces aren't generally seen as part of the class name in the code other than as part of a using directive.

The namespaces are used to logically group related classes, etc. together to help the developer find the correct classes and help minimize the chance of name collisions.

Scott Dorman
I like the idea of not seeing namespaces as part of the class name, now it makes a lot more sense to me. Thanks.
Trap
+3  A: 

I tend to defer to Microsoft's guidance on issues of naming.


Re: Scott's comment.

I think MS is pretty clear on the issue and it is akin to your reply to the OP. For instance:

Do not introduce generic type names such as Element, Node, Log, and Message. There is a very high probability it would lead to type name conflicts in common scenarios. You should qualify the generic type names (FormElement, XmlNode EventLog, SoapMessage).

In the OP's question, the reference was made to the Xml namespace. If you look at the names of those classes in there, they are pretty common names w/out the "Xml" prepended onto them - Attribute, Dictionary, Document, etc.

Anyhow, like I said, I try to follow what they suggest.

itsmatt
Other than the first few sentences, this doesn't actually answer the question. Still a good reference.
Scott Dorman
+1  A: 

I like that the System.Xml classes have the Xml prefix. It helps me distinguish them from a generic counterpart. What's a Document versus XmlDocument? It becomes clearer with the prefix. In my opinion, anyway.

In my own classes, I try to follow this spirit. If I'm taking a somewhat generic word and wrapping it in my class, I may choose to use the namespace as a prefix. For example:

MyProject.Client.Application will collide with Application, which is a .NET class.

MyProject.Client.ClientApplication will avoid collision problems while still remaining clear as to what the class does (at least, in terms of my project).

Robert S.