消除单个文件中的重复代码
遗憾的是,我最近从事的一个项目有大量的复制粘贴代码,即使在单个文件中也是如此。是否有任何工具或技术可以检测单个文件中的重复或接近重复?我有 Beyond Compare 3,它可以很好地比较单独的文件,但我在比较单个文件时不知所措。
提前致谢。
编辑:
感谢所有出色的工具!我一定会检查一下。
这个项目是一个 ASP.NET/C# 项目,但我使用多种语言,包括 Java;我感兴趣的是哪些工具最适合(适用于任何语言)来消除重复。
Sadly, a project that I have been working on lately has a large amount of copy-and-paste code, even within single files. Are there any tools or techniques that can detect duplication or near-duplication within a single file? I have Beyond Compare 3 and it works well for comparing separate files, but I am at a loss for comparing single files.
Thanks in advance.
Edit:
Thanks for all the great tools! I'll definitely check them out.
This project is an ASP.NET/C# project, but I work with a variety of languages including Java; I'm interested in what tools are best (for any language) to remove duplication.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用的是 Eclipse,则可以使用复制粘贴检测器 (CPD) https://olex.openlogic .com/packages/cpd。
If you're using Eclipse, you can use the copy paste detector (CPD) https://olex.openlogic.com/packages/cpd.
你没有说明你正在使用什么语言,这将影响你可以使用什么工具。
对于 Python,有 CloneDigger。它还支持Java,但我还没有尝试过。它可以查找单个文件和文件之间的代码重复,并以 HTML 格式的类似 diff 的报告形式提供结果。
You don't say what language you are using, which is going to affect what tools you can use.
For Python there is CloneDigger. It also supports Java but I have not tried that. It can find code duplication both with a single file and between files, and gives you the result as a diff-like report in HTML.
请参阅 SD CloneDR,这是一个用于检测多个文件内和跨多个文件的复制粘贴编辑代码的工具。它可以检测精确的副本、已重新格式化的副本以及具有不同标识符、文字甚至不同语句序列的几乎未命中的副本。
CloneDR 可处理多种语言,包括 Java(1.4、1.5、1.6)和 C#(尤其是 C#4.0)。您可以在网站上查看示例克隆检测报告,其中还包括一份针对 C# 的报告。
See SD CloneDR, a tool for detecting copy-paste-edit code within and across multiple files. It detects exact copyies, copies that have been reformatted, and near-miss copies with different identifiers, literals, and even different seqeunces of statements.
The CloneDR handles many languages, including Java (1.4,1.5,1.6) and C# especially up to C#4.0. You can see sample clone detection reports at the website, also including one for C#.
Resharper 自动执行此操作 - 它会建议何时应该将代码提取到方法中,并会为您进行提取
Resharper does this automagically - it suggests when it thinks code should be extracted into a method, and will do the extraction for you
配置完后,请查看 PMD (这有点简单)您可以运行其复制粘贴检测器来查找重复的代码。
Check out PMD , once you have configured it (which is tad simple) you can run its copy paste detector to find duplicate code.
具有一定 Office 技能的人可以在 1 分钟内完成以下顺序:
此时,重复的关键字已经被很好地检测到。但要进一步
One with some Office skills can do following sequence in 1 minute:
At this point the keywords for duplicates will be already well detected. But to go further
有一个分析工具,叫做Simian,我还没有尝试过。据说它可以在任何类型的文本上运行并指出重复的项目。它可以通过命令行界面使用。
There is an analysis tool, called Simian, which I haven't yet tried. Supposedly it can be run on any kind of text and point out duplicated items. It can be used via a command line interface.
另一个选项与上述类似,但具有不同的工具链: https://www.npmjs.com/包/jscpd
Another option similar to those above, but with a different tool chain: https://www.npmjs.com/package/jscpd