views:

219

answers:

5

Hi,

Is it a good idea to use "system namespaces" in my class libraries?

Sample:

namespace System.Web {
    public static class RequestExtensions {
        public static bool IsPost(this HttpRequest r) {
            return string.Compare(r.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0;
        }
    }
}

The advantage: no need to include additional uses-clauses (especially for extension methods), so all becomes available straight after adding reference to the library.

The best sample is NUnitEx project (which uses NUnit's namespace).

Disadvantages: potential name conflicts.

+3  A: 

Very very bad. It's confusing, and you should only do it if you absolutely have to (there are some cases when it's needed).

Only ever do it when it's 100% required, don't ever do it just for 'convenience'.

Noon Silk
A: 

Using a System-based namespace can make it harder for someone to pick up your code and figure out what it's doing. For instance, if I pick up new C#, I often end up Googling things like "System.Web.xyz" when I don't know something.

In this case, I probably wouldn't know that "System.Web.RequestExtensions" wasn't a real member of the System.Web namespace, so I'd get stuck looking for a class that doesn't exist.

So basically, my view is that you need to document it really well or find another namespace.

Matthew Iselin
+2  A: 

I think it is not good idea, because may be Microsoft will decide to create RequestExtensions class in the next versions of framework, It is always good practice to start namespace with your company name to prevent name conflicts

ArsenMkrt
-1: This only works if the future type has *exactly* the same methods and semantics as your version. This is rather unlikely (to say the least).
Richard
That would entirely depend. I once created a WCF RoutingService based on an MSDN article. If I had made the poor choice to stick it in the System.ServiceModel namespace, it would have conflicted with the .NET 4.0 RoutingService due for final release next year. Unlikely is highly subjective and rather chancy.
jrista
+7  A: 

I have to second everyone else who says its a BAD idea. Namespaces are an organizational tool, on many levels. Not only do they allow you to reuse identifiers for your own purposes without conflicting with other companies, they also allow different companies to isolate their product from your's or anyone else's. Putting code into the System namespace can be very confusing for the people who use your types.

Additionally, people know that anything in a System namespace is good, solid, tested, community vetted, thoroughly documented code that came from Microsoft. Thats a pretty important set of factors to live up to...by sticking your code in the same namespace, not only are you claiming your code is that good, but you have to live up to it.

jrista
+2  A: 

The design guidelines talk about namespace naming:

The general format for a namespace name is as follows:

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

For example, Microsoft.WindowsMobile.DirectX.

Do prefix namespace names with a company name to prevent namespaces from different companies from having the same name and prefix.

There is no scope for reusing System or Microsoft here.

Richard