What's the value in removing and/or sorting Usings?

702 Views Asked by At

I've always run Remove and Sort Usings as a matter of course, because it seems the right thing to do. But just now I got to wondering: Why do we do this?

Certainly, there's always a benefit to clean & compact code. And there must be some benefit if MS took the time to have it as a menu item in VS.

Can anyone answer: why do this? What are the compile-time or run-time (or other) benefits from removing and/or sorting usings?

2

There are 2 best solutions below

0
On BEST ANSWER

As @craig-w mentions, there's a very small compile time performance improvement.

The way that the compiler works, is when it encounters a type, it looks in the current namespace, and then starts searching each namespace with a using directive in the order presented until it finds the type it's looking for.

There's an excellent writeup on this in the book CLR Via C# by Jeffrey Richter (http://www.amazon.com/CLR-via-4th-Developer-Reference/dp/0735667454/ref=sr_1_1?ie=UTF8&qid=1417806042&sr=8-1&keywords=clr+via+c%23)

As to why MS provided the menu option, I would imagine that enough internal developers were asking for it for the same reasons that you mention: cleaner, more concise code.

0
On

There's probably a teeny-tiny (i.e. minuscule/virtually unmeasurable) performance improvement during compilation because it doesn't have to search through namespaces that you aren't actually using for unqualified types. I do it because it's just neater and easier to read in the end. Also, I use the Productivity Power Tools and have them set to do the Remove and Sort when I save the file.