简体   繁体   English

冗余代码检查

[英]Redundant Code Checking

Are there any tools that can find any private functions without any references? 有没有可以找到没有任何引用的私有函数的工具? (Redundant functions) (冗余功能)

Reason being, that a function may have been created and called from a couple of areas, but as the project expands and grows, these two calls may have been removed and swapped with a better alternative. 原因是,可能已经从几个区域创建并调用了一个函数,但是随着项目的扩展和增长,这两个调用可能已被删除并替换为更好的选择。 But the method may still remain. 但是该方法可能仍然存在。 I was wondering if there were any handy tools that would look through the code, spotting private functions and checking if they have any references, if not, inform the user of the situation. 我想知道是否有任何方便的工具可以检查代码,发现私有函数并检查它们是否有引用,如果没有,则将情况告知用户。

It wouldn't be too tricky to create one myself, but I was wondering if there were any accessible apps that could do this with the files containing the code? 自己创建一个应用不是很棘手,但是我想知道是否有任何可访问的应用程序可以对包含代码的文件执行此操作?

My code is in c#, but I can imagine that this question covers a variety of coding languages. 我的代码是用c#编写的,但是我可以想象这个问题涵盖了多种编码语言。

ReSharper可以完成这项工作。

If your code has Unit Tests (it does, right? ;-) then running NCover will allow you to identify methods that aren't being called from anywhere. 如果您的代码具有单元测试(是的,对吗?;-),那么运行NCover将使您能够识别未从任何地方调用的方法。 If you haven't got any unit tests then it's a good excuse to use for starting to build them. 如果您没有任何单元测试,那么这是开始构建它们的很好的借口。

In the general case, I'd suspect that code coverage tools are a good fit in most languages. 在一般情况下,我怀疑代码覆盖率工具是否适合大多数语言。

Eclipse会自动针对Java执行此操作,不确定C#是否可以使用相同的工具。

Another question might even be "Does the c# compiler remove private methods that aren't actually used?". 另一个问题甚至可能是“ C#编译器会删除实际上未使用的私有方法吗?”。

My guess would be no, but you never know! 我的猜测不会,但您永远不会知道!

EDIT: 编辑:

Actually, I think it might be hard to tell where a method is used. 实际上,我认为可能很难分辨出在哪里使用了一种方法。 It might be private, but it can still be used as Event Handlers. 它可能是私有的,但仍可以用作事件处理程序。 Not impossible to check, but I'm sure that aspect would make it a little more difficult. 并非没有可能进行检查,但是我敢肯定,这方面会使它变得更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM