Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Once when I was refactoring my code, I went up the IDE to the using section of my C# class, and cleaned unused namespaces and duplicate namespaces, and sorted them all.

My pair (pair programming) asked me the reason. I had no idea why I did that. I did it out of my habit to keep all of my code clean and tidy. I mean, I told him that having a cleaner code is a good idea in general, but of course that reason was not a good justification, as I won't even bother to spend my time in the using section of any C# code page.

Since many times you move a class or an enum (or a type in general) from one namespace to another namespace, and this adds new using statements to your code (either manually by going up the code window and writing the using statement yourself, or through the editor using Alt+Ctrl+F10 combination), and since these new using statements would be added to the end of using section, which makes them not sorted alphabetically, and since compiler never complaints about any of these problems, why should we care to make this section clean and tidy? What reasons might we have?

share|improve this question
5  
This has already been asked and answered on Stack Overflow - stackoverflow.com/questions/4163320/unused-using-statements (and the two linked questions on the side bar) - basically there's no reason to worry about unused usings –  ChrisF Apr 22 '12 at 10:36
    
Great reference @ChrisF, I think I got my answer there. Maybe you should join question with that one, or do something similar. :) –  Saeed Neamati Apr 22 '12 at 11:05
    
Check this out also: stackoverflow.com/a/136646/333306 –  user2567 Apr 22 '12 at 15:11
    
stackoverflow.com/questions/136278/… –  user Apr 29 '12 at 15:56

5 Answers 5

My main reasons to clean up amongst the using statements are:

  • The more using statements the more possibility of naming conflicts which would mean you have to include parts of the namespace in the code to avoid ambiguity.
  • The IntelliSense is filtered based on all the assemblies in your using statements. Therefore if you clean it up from unnecessary statements you'll help yourself by helping with the accuracy of the IntelliSense.

On top of that I agree with the other answers in that it increases readability and makes it easier to get an idea of what the type(s) in the class do.

share|improve this answer

“Perfection is achieved not when there is nothing left to add, but when there is nothing left to take away” – Antoine de Saint-Exupery

Whenever you can remove something that is not necessary and does not add understanding, do so (readability is worth extra code).

share|improve this answer
    
Great aphorism. I liked the idea. +1 ;) –  Saeed Neamati Apr 24 '12 at 3:41
  • It improves readability of your code.
  • Normally it makes little sense to follow this guideline if you have just few using statements

  • It makes more sense to separate using statements into sections.

For example:

    using System.Web;

    using MyPlatform.FooX;        
    using MyPlatform.FooY;

    using MyFramework.Helpers;        
    using MyFramework.Extentions;

If I look at the class, then I can instantly see that given class is using System.Web assembly, as well as our platform and framework. This gives me a rough idea of its dependencies and complexity.

You can then take this a step further and order the statements, but I think that it makes using statements less readable, so I wouldn't recommend this.

using MyFramework.Extentions;
using MyFramework.Helpers;

using System.Web;

using MyPlatform.FooX;        
using MyPlatform.FooY;
share|improve this answer

It's just removing noise from the signal. Less noise means it's easier to receive the signal, i.e. understand the intent of the code.

As a generator of noise it's a pretty minor one though.

share|improve this answer

There is no performance difference, no matter how many using directives you have.

But I think it makes sense to keep them clean for two reasons:

  1. If you look at the usings, you can see what dependencies does the file have. This can help you figure out what the type(s) in the file do. If you this, having the usings in a specific order helps you see it faster.
  2. If you have too many usings, it may indicate that you have poor separation of concerns and that the type(s) in the file do too much.

Both of these aren't very important, so you shouldn't worry about it too much. But personally, I think it makes it worth keeping the usings clean.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.