是否有一个自定义的FxCop规则,能够检测未使用的公共方法?自定义、有一个、规则、方法

2023-09-02 10:51:12 作者:一梦べ少年蓝

我只是尝试的FxCop。它确实检测未使用的私有方法,但不使用的公众。是否有一个自定义规则,我可以下载,插件将检测公共方法不是从同一程序集?

I just tried FxCop. It does detect unused private methods, but not unused public. Is there a custom rule that I can download, plug-in that will detect public methods that aren't called from within the same assembly?

推荐答案

科里,我使用的FxCop的答案都假定你有兴趣在删除未使用的私有成员,但是解决与其他情况下,你可以尝试使用问题的 NDepend的。下面是一些CQL检测未使用的公共成员(改编自下面列出的文章):

Corey, my answer of using FxCop had assumed you were interested in removing unused private members, however to solve the problem with other cases you can try using NDepend. Here is some CQL to detect unused public members (adapted from an article listed below):

// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
 MethodCa == 0 AND            // Ca=0 -> No Afferent Coupling -> The method 
                              // is not used in the context of this
                              // application.

 IsPublic AND                 // Check for unused public methods

 !IsEntryPoint AND            // Main() method is not used by-design.

 !IsExplicitInterfaceImpl AND // The IL code never explicitely calls 
                              // explicit interface methods implementation.

 !IsClassConstructor AND      // The IL code never explicitely calls class
                              // constructors.

 !IsFinalizer                 // The IL code never explicitely calls
                              // finalizers.

来源:帕特里克Smacchia的$ C $的耦合,死code,设计缺陷和再设计Ç指标。文章还都要通过检测死领域和类型。

Source: Patrick Smacchia's "Code metrics on Coupling, Dead Code, Design flaws and Re-engineering. The article also goes over detecting dead fields and types.

(编辑:回答说更容易理解)的

2012编辑6月11日:解释关于未使用code新NDepend的设施。免责声明:我是这个工具的开发者之一的

EDIT 11th June 2012: Explain new NDepend facilities concerning unused code. Disclaimer: I am one of the developer of this tool.

由于NDepend的2012年5月V4发布,该工具建议写 code在LINQ查询(CQLinq)规则。各地均提出 200默认code规则,其中3人被专用于未使用/死code 的检测:

Since NDepend v4 released in May 2012, the tool proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection:

潜在的死亡类型(因此检测未使用的类,结构,接口,代表...) 潜在的死法 (因此检测未使用的方法,构造函数,属性的getter / setter方法​​...) 潜在的死域 Potentially dead Types (hence detect unused class, struct, interface, delegate...) Potentially dead Methods (hence detect unused method, ctor, property getter/setter...) Potentially dead Fields

这些CQLinq code规则比previous CQL的人更加强大。如果您对以上这些规则的来源$ C ​​$ c。单击这3个环节,你会看到,关于类型和方法的是一个有点复杂。这是因为它们不仅检测所用的仅的由未使用的死类型和方法(递归)

These CQLinq code rules are more powerful than the previous CQL ones. If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).

这是的的静态分析的,因此preFIX的潜在的在规则的名称。如果code元素用于的只有的通过反射,这些规则可能会认为这是不使用的是情况并非如此。

This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.

在除了使用这些3的规则,我建议测量code覆盖率的测试,争创具有全覆盖。通常情况下,你会看到,code不能覆盖测试,实际上的未使用/死的code,它可以安全地丢弃。这是在复杂的算法,其中还不清楚如果code分支可达与否尤为有用。

In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.