消除单个文件中的重复代码

发布于 2024-08-20 02:50:54 字数 288 浏览 3 评论 0原文

遗憾的是,我最近从事的一个项目有大量的复制粘贴代码,即使在单个文件中也是如此。是否有任何工具或技术可以检测单个文件中的重复或接近重复?我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

清风挽心 2024-08-27 02:50:54

如果您使用的是 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.

风启觞 2024-08-27 02:50:54

你没有说明你正在使用什么语言,这将影响你可以使用什么工具。

对于 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.

云仙小弟 2024-08-27 02:50:54

请参阅 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#.

倦话 2024-08-27 02:50:54

Resharper 自动执行此操作 - 它会建议何时应该将代码提取到方法中,并会为您进行提取

Resharper does this automagically - it suggests when it thinks code should be extracted into a method, and will do the extraction for you

盛夏尉蓝 2024-08-27 02:50:54

配置完后,请查看 PMD (这有点简单)您可以运行其复制粘贴检测器来查找重复的代码。

Check out PMD , once you have configured it (which is tad simple) you can run its copy paste detector to find duplicate code.

梦年海沫深 2024-08-27 02:50:54

具有一定 Office 技能的人可以在 1 分钟内完成以下顺序:

  • 使用普通格式化程序统一代码风格,最好不要换行将
  • 代码文本作为单列
  • 搜索输入 Microsoft Excel 并将所有双空格替换为单空格并进行其他替换
  • 排序列

此时,重复的关键字已经被很好地检测到。但要进一步

  • 将比较器公式添加到第二列,并
  • 再次计数器到第三个复制和粘贴值,排序并查看最重复的行

One with some Office skills can do following sequence in 1 minute:

  • use ordinary formatter to unify the code style, preferably without line wrapping
  • feed the code text into Microsoft Excel as a single column
  • search and replace all dual spaces with single one and do other replacements
  • sort column

At this point the keywords for duplicates will be already well detected. But to go further

  • add comparator formula to 2nd column and counter to 3rd
  • copy and paste values again, sort and see the most repetitive lines
千紇 2024-08-27 02:50:54

有一个分析工具,叫做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.

无悔心 2024-08-27 02:50:54

另一个选项与上述类似,但具有不同的工具链: https://www.npmjs.com/包/jscpd

Another option similar to those above, but with a different tool chain: https://www.npmjs.com/package/jscpd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文