tags:

views:

131

answers:

8

I am looking at code which has the correct using statements declared but yet the objects within the code are fully qualified when they don't need to be due to the fact that the using statement is declared.

Is there a reason why the objects are fully qualified but yet still have the using statement declared?

+3  A: 

Perhaps the using statement was added after the objects were fully qualified? Another less likely reason is that there are namespace conflicts with the objects in use.

Mayo
A: 

There are lots of reasons. A few might be:

  1. People tend to lean on Intellisense to find the classes they are looking for.
  2. Code has been pasted from elsewhere.
  3. Bad habits die hard.

A good question is whether there is a way that exists in Visual Studio to eliminate this redundancy. I am not aware of such a tool (probably something like CodeRush), but perhaps someone will comment here with one.

Jerry Bullard
ReSharper can do that.
Martinho Fernandes
Yep, CodeRush/Refactor! Pro does it.
Ahmad Mageed
I contest the 'bad habbit'. (-:
Henk Holterman
+1  A: 

Sometimes namespaces 'conflict' - there are classes of the same name in multiple namespaces and fully-qualifying them distinguishes them.

n8wrl
+1  A: 

It may be because there are conflicting names within two imported namespaces.

Say, A.A has a type called Foo (A.A.Foo), and B.B has a type called Foo (B.B.Foo). If you do this:

using A.A;
using B.B;

// class definitions... etc
    var x = new Foo(); // which foo?

You could do this if you don't want to fully qualify it:

using A.A;
using B.B;
using AFoo = A.A.Foo;
using BFoo = B.B.Foo;

// class definitions... etc
    var x = new AFoo();

Why not simply remove the using B.B; statement? Well, supposed you're also using types B.B.Bar, A.A.FooBar, B.B.Qux and A.A.Quux. You would want to keep the using statements then.

Martinho Fernandes
A: 

When you use some of the built in refactorings they sometimes fully qualify the object. This is safer to avoid compiler confusion.

John Nolan
A: 

I can think of a few reasons:

  1. there are multiple using statements, either nested or included on the same line, and there is some ambiguity as to which object the method property belongs to
  2. the using block is particularly long and the statement itself might be off screen. Qualifying the methods and properties can make it more readable for other programmers.
  3. there might be some very junior programmers on the project who might not quite understand the using statement and the programmer might be trying to make it more readable for them.
Jeff Hornby
+2  A: 

Why some people (like me) do it intentionally:

When using a relatively rare class, it provides a lot of information about the class. And I like puting information in the code. Consider:

System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = 
     new SoapFormatter();  // .NET 2

or

var formatter = new  // .NET 3
   System.Runtime.Serialization.Formatters.Soap.SoapFormatter.SoapFormatter();

I am fully aware about the inconsistency and that the 'when to use' is kind of arbitrary. But for somebody reading this code a lot of questions are answered before they come up.

And Intellisense could answer the same questions but it is not always available.

Henk Holterman
I'm not so sure that the benefit gained by the extra information outweighs the readability problem it introduces. Personally, it is one of my bugbears to see fully qualified tokens when there is no need for them and I think it is because it looks messy and harder to read. Let's face it, intelligence is usually available. Obviously if it is required for the various reasons mentioned here then you have to deal with it. My desire for compiling code outweighs readable code, just.
Andy McCluggage
@Andy - I disagree. For people not familiar with the particular namespace, especially with code that references a whole bunch of namespaces, the above does make the code easier to read. And while I've tried, I've never been able to get intellisense to work on a printout.
Jeff Hornby
We clearly just work in different ways, Jeff. I can honestly say that I very rarely need to know the full namespace of a class, and this doesn’t often affect my ability to use it properly. Of course it has to be named well, but most classes are (in the .Net Framework at least). I think we will have to agree to disagree on this, although I can say for definite that I never find myself printing code, even as part of a review process.
Andy McCluggage
A: 

One reason this can happen: auto-generated code. (like the Windows Forms designer-generated code)

Tommy Carlier