为什么要在c#中删除未使用的指令?

[英]Why remove unused using directives in C#?


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?

我想知道除了整理源代码之外,为什么开发人员在Visual Studio 2008中使用“移除未使用的”特性?

10 个解决方案

#1


170  

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!

另一方面,没有太多的理由让他们留在那里。我想你省掉了删除它们的麻烦。但是如果你很懒,你就会有更大的问题!

#2


24  

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.

假设您必须在3个月、6个月、9个月后回到您的代码—或者其他人必须接管您的代码并维护它。

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

马克

#3


14  

This seems to me to be a very sensible question, which is being treated in quite a flippant way by the people responding.

在我看来,这是一个非常敏感的问题,人们对它的反应相当轻率。

I'd say that any change to source code needs to be justified. These changes can have hidden costs, and the person posing the question wanted to be made aware of this. They didn't ask to be called "lazy", as one person inimated.

我想说,对源代码的任何修改都需要被证明是合理的。这些变化可能有隐藏的代价,提出问题的人想要知道这一点。他们不要求别人叫他们“懒”,就像一个人说的那样。

I have just started using Resharper, and it is starting to give warnings and style hints on the project I am responsible for. Amongst them is the removal of redundant using directive, but also redundant qualifiers, capitalisation and many more. My gut instinct is to tidy the code and resolve all hints, but my business head warns me against unjustified changes.

我刚刚开始使用Resharper,它开始对我负责的项目给出警告和样式提示。其中之一是删除冗余使用指令,但也有冗余限定符、大小写等等。我的直觉是整理代码并解决所有的提示,但我的业务主管警告我不要做出不合理的更改。

We use an automated build process, and therefore any change to our SVN repository would generate changes that we couldn't link to projects/bugs/issues, and would trigger automated builds and releases which delivered no functional change to previous versions.

我们使用一个自动构建过程,因此对SVN存储库的任何更改都将生成无法链接到项目/bug /问题的更改,并将触发自动构建和发布,对以前的版本没有任何功能更改。

If we look at the removal of redundant qualifiers, this could possibly cause confusion to developers as classes our Domain and Data layers are only differentiated by the qualifiers.

如果我们考虑删除冗余限定符,这可能会使开发人员感到困惑,因为我们的域和数据层仅由限定符区分。

If I look at the proper use of capitalisation of anachronyms (i.e. ABCD -> Abcd) then I have to take into account that Resharper doesn't refactor any of the Xml files we use that reference class names.

如果我考虑使用anachronyms(即ABCD -> ABCD)的恰当使用,那么我必须考虑到Resharper不会重构任何我们使用的引用类名称的Xml文件。

So, following these hints is not as straight-forward as it appears, and should be treated with respect.

因此,遵循这些提示并不像看上去的那么直接,应该受到尊重。

#4


12  

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

智能感知弹出窗口中的选项较少(特别是如果名称空间包含大量扩展方法)。

Theoretically Intellisense should be faster too.

理论上,智能感知也应该更快。

#5


11  

In addition to the reasons already given, it prevents unnecessary naming conflicts. Consider this file:

除了已经给出的原因之外,它还避免了不必要的命名冲突。考虑一下这个文件:

using System.IO;
using System.Windows.Shapes;

namespace LicenseTester
{
    public static class Example
    {
        private static string temporaryPath = Path.GetTempFileName();
    }
}

This code doesn't compile because both the namespaces System.IO and System.Windows.Shapes each contain a class called Path. We could fix it by using the full class path,

由于名称空间系统,此代码不进行编译。IO和System.Windows。每个形状都包含一个名为Path的类。我们可以用全类路径来修正,

        private static string temporaryPath = System.IO.Path.GetTempFileName();

or we could simply remove the line using System.Windows.Shapes;.

或者我们可以使用System.Windows.Shapes删除这条线。

#6


5  

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.

移除它们。更少的代码去查看和思考可以节省时间和混乱。我希望更多的人能保持简单、整洁。就像你的房间里有脏衬衫和裤子一样。它很丑,你必须想知道它为什么在那里。

#7


4  

It also helps prevent false circular dependencies, assuming you are also able to remove some dll/project references from your project after removing the unused usings.

它还有助于防止错误的循环依赖,假设您还能够在删除未使用的内容之后从项目中删除一些dll/项目引用。

#8


2  

Code compiles quicker.

代码编译更快。

#9


2  

At least in theory, if you were given a C# .cs file (or any single program source code file), you should be able to look at the code and create an environment that simulates everything it needs. With some compiling/parsing technique you may even create a tool to do it automatically. If this is done by you at least in mind, you can ensure you understand everything that code file says.

至少在理论上,如果您得到一个c# .cs文件(或任何一个程序源代码文件),您应该能够查看代码并创建一个环境来模拟它需要的一切。使用一些编译/解析技术,您甚至可以创建一个工具来自动执行。如果这是您至少在头脑中完成的,您可以确保您理解代码文件所述的所有内容。

Now consider, if you were given a .cs file with 1000 using directives which only 10 was actually used. Whenever you look at a symbol that is newly introduced in the code that references the outside world, you will have to go through those 1000 lines to figure out what it is. This is obviously slows down the above procedure. So if you can reduce them to 10, it will help!

现在考虑一下,如果给您一个.cs文件,其中使用了1000条指令,而实际上只使用了10条指令。每当您看到代码中引用外部世界的新引入的符号时,您就必须遍历这1000行才能知道它是什么。这显然减慢了上述过程。所以如果你能把它们减少到10个,就会有帮助!

In my opinion, the C# using directive is very very weak, since you cannot specify single generic symbol without genericity being lost, and you cannot use using alias directive to use extension methods. This is not the case in other languages like Java, Python and Haskell, in those languages you are able to specify (almost) exactly what you want from the outside world. But event then, I will suggest to use using alias whenever possible.

在我看来,c#使用指令非常弱,因为您不能在不丢失泛型的情况下指定单个泛型符号,也不能使用alias指令来使用扩展方法。在Java、Python和Haskell等其他语言中,情况并非如此,在这些语言中,您可以(几乎)精确地指定您想从外部世界得到什么。但是,如果可能的话,我建议使用别名。

#10


2  

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.

想象你有两个组件,引用另一个(现在我们´s调用第一个A和B)引用。当你有代码取决于B一切都很好。但是在某个阶段你在开发过程中注意´你真的不需要这段代码,但你离开的使用叙述。现在你不仅有一个毫无意义的使用指令,而且还有一个对B的集合引用它在任何地方都没有使用,但是在过时的指令中。这首先增加了编译A所需的时间,因为B也必须被加载。

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.

最后在我们的exapmle中,我们必须将B和A一起运送,虽然B在A中没有使用,但是在使用部分。这将极大地影响加载程序集时A的运行时性能。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/03/10/72064a3458ff36899689f1176653e8da.html



猜您在找
C#中未使用的“使用”指令的性能影响 - Performance impact of unused “using” directives in C# 如何使用名称空间删除未使用的名称空间 - How to remove unused using namespaces 如何自动查找未使用的#include指令? - How do I automate finding unused #include directives? 在C#中维护使用指令的“干净”列表有什么好处? - What are the benefits of maintaining a “clean” list of using directives in C#? 如何从composer中删除未使用的依赖项? - How to remove unused dependencies from composer?
智能推荐
 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告