Why remove unused using directives in C#?

后端 未结 10 1601
猫巷女王i
猫巷女王i 2020-11-30 20:10

I\'m wondering if there are any reasons (apart from tidying up source code) why developers use the \"Remove Unused Usings\" feature in Visual Studio 2008?

相关标签:
10条回答
  • 2020-11-30 20:16

    There are a few reasons you'd want to take them out.

    • It's pointless. They add no value.
    • It's confusing. What is being used from that namespace?
    • If you don't, then you'll gradually accumulate pointless using statements as your code changes over time.
    • Static analysis is slower.
    • Code compilation is slower.

    On the other hand, there aren't many reasons to leave them in. I suppose you save yourself the effort of having to delete them. But if you're that lazy, you've got bigger problems!

    0 讨论(0)
  • 2020-11-30 20:24

    Less options in the Intellisense popup (particularly if the namespaces contain lots of Extension methods).

    Theoretically Intellisense should be faster too.

    0 讨论(0)
  • 2020-11-30 20:26

    Remove them. Less code to look at and wonder about saves time and confusion. I wish more people would KEEP THINGS SIMPLE, NEAT and TIDY. It's like having dirty shirts and pants in your room. It's ugly and you have to wonder why it's there.

    0 讨论(0)
  • 2020-11-30 20:31

    I would say quite the contrary - it's extremely helpful to remove unneeded, unnecessary using statements.

    Imagine you have to go back to your code in 3, 6, 9 months - or someone else has to take over your code and maintain it.

    If you have a huge long laundry list of using statement that aren't really needed, looking at the code could be quite confusing. Why is that using in there, if nothing is used from that namespace??

    I guess in terms of long-term maintainability in a professional environment, I'd strongly suggest to keep your code as clean as possible - and that includes dumping unnecessary stuff from it. Less clutter equals less confusion and thus higher maintainability.

    Marc

    0 讨论(0)
  • 2020-11-30 20:31

    Recently I got another reason why deleting unused imports is quite helpful and important.

    Imagine you have two assemblies, where one references the other (for now let´s call the first one A and the referenced B). Now when you have code in A that depends on B everything is fine. However at some stage in your development-process you notice that you actually don´t need that code any more but you leave the using-statement where it was. Now you not only have a meaningless using-directive but also an assembly-reference to B which is not used anywhere but in the obsolete directive. This firstly increases the amount of time needed for compiling A, as B has to be loaded also.

    So this is not only an issue on cleaner and easier to read code but also on maintaining assembly-references in production-code where not all of those referenced assemblies even exist.

    Finally in our exapmle we had to ship B and A together, although B is not used anywhere in A but in the using-section. This will massively affect the runtime-performance of A when loading the assembly.

    0 讨论(0)
  • 2020-11-30 20:31

    Code compiles quicker.

    0 讨论(0)
提交回复
热议问题