為什么要在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的運行時性能。


注意!

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



 
粤ICP备14056181号  © 2014-2020 ITdaan.com