Namespace Pollution / Collisions

Today I’m going to talk about namespace pollution and when I feel it really comes into play. I have done some reading and research on the subject because although I always had an idea of what namespace pollution (also known as namespace collision) was based on its name, I never really read a definition on it or people’s thoughts on the subject. That being said, there really wasn’t a bunch on it’s explanation per se, more examples and after the last post, we know how I feel about examples without definitions or analogies.

So lets start with what is namespace pollution. Well, in essence namespace pollution is when a developer (really it most likely starts at the architect level) creates objects or namespaces that collide with namespaces that already exist in the .NET framework assemblies. So why might this be a problem? In a word, Ambiguity.

How to Fix the Ambiguity

Lets say for instance that you were creating a .NET web application and while building out the various classes you would need to create a User class that will abstract the HttpContext and various other user credential information you may need. Well, it just so happens that there is a class in the ASP.NET framework called User. Now in this case, you have a few options. If you are always going to use your User object outside of the HttpContext.User object in .NET, you can just add the using at the top of the page. This will tell the compiler the class you wish to use and remove the ambiguity.

The other option can be used if you are going to use both versions of User in the same class file. You have to specify the full namespace path to the version you wish to use each time you use it. for example, if your full namespace path to the User object is GreggsSoftware.AwesomeApp.User, this is what you would need to put on every line of code in t he class you are using both User objects to verify to the compiler what you want to use.

My Thoughts (For what it’s worth)

Now, my first thoughts on this is, “Do you really need to name something the same as another object in the .NET Framework?”. My answer, No! I think if you are going to be making an object that is specific to your project, then name it as such. GoogleUser, AmazonUser, BookmarkUser… You get the idea. This will make it unique to your users when they reference it and decipherable to developers using your API. it also makes your code more readable in that you aren’t declaring the whole namespace path every time you use the object.

The global Keyword

Now here is something interesting I didn’t know about. There is a keyword in .NET called Global. This keyword allows you to directly access the global namespace (aka .NET namespace (System.*). Now you may be wondering when you may want to use such a thing? Well, in my last job, we actually could have used this. One of the developers working on my team was building a set of tables and associated classes in a library for an internal application for a customer. Letting him run with it, we unfortunately did not have time for a code review prior to development and he created a table in the database called Systems and a class called System in the .NET library. Now, this caused an immediate problem. First was that we should have had a code review, and I won’t go into how aggravated I got at my last job for not thinking they were cost effective, but I digress. The second problem was that when you use a name like System, you now create ambiguity on the whole .NET framework that relies on the System namespace (which is A LOT of .NET assemblies).

To fix this, we can use the global keyword which will specify that you wish to use the .NET frameworks System and not the System you created by mistake. Here is an example of how you would use the keyword to call System.Console.WriteLine.

global::System.Console.WriteLine("This will work just fine");

Here is the link to the MSDN examples of the global keyword Click here.

Obviously, as MSDN mentions in the above article, creating your own System namespace is highly frowned upon and not recommended whatsoever, but if you inherit code that does it or run into a situation that it becomes necessary, there is hope.


Happy Coding!1 Smile


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s