18

Are there any benefits to removing unused using statements in a VS project (such as can be done using Resharper), or will VS automatically take care of that when building/deploying?

  • 1
    Having the impression that you asked about removing unused references (DLLs) and all the answers are about removing unused using statements. – Răzvan Flavius Panda Nov 21 '14 at 10:14
  • @RăzvanPanda: No, it is using statements; I'll have to czech if you can do the latter with R# - that would be pleasantly low temp, too. – B. Clay Shannon Nov 21 '14 at 13:47
  • Aight, correcting the wording to fit the meaning. – Răzvan Flavius Panda Nov 21 '14 at 15:06
  • 1
    you can remove unused DLL references with latest R#, but it is important to be careful about dynamic DLL references or things might break at runtime. – Răzvan Flavius Panda Nov 21 '14 at 15:45
21

There aren't any performance benefits, if that's what you mean.

All references in an assembly are fully qualified; the compiler merely uses the references you provide in your code to fully qualify identifiers, so the only impact of unused references in your source code is a slight decrease in readability (why is this reference here?), and a trivial increase in compile time.

To put it another way, the generated IL is exactly the same whether you remove the unused references or not.

  • There can be an indirect benefit to removing unused Usings. If doing so in turn allows one or more references to external DLLs to be removed from a solution, that could benefit the application's deployment size and/or startup time. – weir Jul 23 '14 at 20:55
  • 1
    The generated IL doesn't change if you remove usings, so I don't see how anything would be affected. – Robert Harvey Jul 23 '14 at 20:56
  • True: IL will not change if you add unused usings or DLL references (Reference Include MSBuild file element, /reference: CSC argument). I had in mind: Remove unused usings from a Web Application Project. Those had been the only lines of code compelling inclusion of a bunch of DLL references, so now remove those. As you say, builds faster- more so if references were UNC paths (CSC checks all reference paths). Trivial win 2: Speed of CI (scripts grab all referenced DLLs) improves as fewer files are copied e.g. from source control server to build server to publish server to app server(s). – weir Jul 24 '14 at 18:34
  • Shorter code is more readable code. Less is more - this is the benefit & it's worth doing for that reason. – niico Mar 16 '18 at 13:48
23

Yes - I can think of two primary benefits:

  1. Beyond its primary functional purpose (i.e. to reduce code verbosity), the 'Using' statement list at the top of a code file can tell future readers (especially those without Resharper) which namespaces are (or at least were) relevant to that code file. If you actively prune this list, it can act as a better signaling mechanism.
  2. Removing unused namespaces will reduce the number of autocompletion candidates in your text editor as you type. If you rely on the autocompletion lists at all, this will help keep you "on the straight and narrow" and may even increase your typing speed, as you should be able to find your intended autocompletion candidate just a little bit faster.
  • 1
    Ah, very good points, especially #2! – B. Clay Shannon Jun 30 '13 at 4:56
-1

Removing unused code, is just extra baggage and hard to measure the efficiencies gained.

  • Removing unused code, brings down the compilation time, I am working on a project which takes anywhere from 15 to 20 minutes, there is tons of unused code. Eliminating unused code decreased the compile time to ~7 minutes. This makes a diff when there are many developers on the team, this saves everyone time to quickly build and test.

  • If you have automated your build and unit tests, you have now removed unit tests you do not need. again this is reduced time taken to run unit tests. Just increased efficiency in your Continuous Build management process.

    • Unused code results in bigger foot print of your binaries. Having a smaller code foot print will reduce the time taken to load/start up initially. Though the gains may be very meager and subjective.
  • 4
    the question is not about unused code in general, it's about unused references – gnat Oct 29 '14 at 16:15
  • ...see How to Answer: "Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that – or a viable alternative..." – gnat Oct 29 '14 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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