Linux下如何处理重复代码?
我正在寻找处理大约 15 万行代码的遗留 PHP 项目中重复代码的最佳方法。
这是最好的手动解决方法还是有独立的重复代码检测器可以减轻痛苦?
I'm looking for the best approach to dealing with duplicate code in a legacy PHP project with about 150k lines of code.
Is this something best approached manually or are there standalone duplicate code detectors that will ease the pain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他答案已经提到的那样,这应该手动处理,因为您可能希望在进行过程中更改其他内容以使代码库更干净。
也许实际的调用已经是多余的,或者可以组合类似的片段。
此外,在实践中,人们通常会稍微更改复制的代码,因此通常不会出现直接重复,而是接近的变体。 我担心自动 c&p 检测大多会让你失败。
然而,有一些重构工具可以帮助您实际执行更改(有时还可以找到可能的候选者)。 Google 搜索“php refactoring”,有很多可用的工具,既有独立的,也有作为 IDE 的一部分。
As the other answers already mention, this should be approached manually, because you may want to change other things as you go along to make the code base cleaner.
Maybe the actual invocation is already superfluous, or similar fragments can be combined.
Also, in practice people usually slightly change the copied code, so there will often not be direct duplicates, but close variants. I fear automatic c&p detection will mostly fail you there.
There are however refactoring tools that can help you with acutally performing the changes (and sometimes also with finding likely candidates). Google for "php refactoring", there are quite a few tools available, both standalone and as part of IDEs.
CloneDR 可以在大型源系统中查找重复的代码,包括精确的副本和未遂的副本,参数化通过语言语法。 对于每组检测到的克隆,它甚至会提出可用于替换克隆的抽象代码草图。
它适用于多种语言,包括 PHP 系统。 可在链接中找到 Joomla(PHP 框架)的示例 PHP 克隆检测报告。
The CloneDR finds duplicate code, both exact copies and near-misses, across large source systems, parameterized by langauge syntax. For each detected set of clones, it will even propose a sketch of the abstraction code that could be used to replace the clones.
It is available for many langauges, including PHP system. A sample PHP clone detection report for Joomla (a PHP framework) can be found at the link.
另请考虑导致此代码重复的过程!
如果您必须更改代码,大多数情况下,复制代码比重构代码更快,这样它既可以用于您的新用途,也可以用于您的旧用途。
因此,您必须让人们相信重构比简单的复制更好,因为它从长远来看(而不是短期)可以节省时间。
否则你会发现自己在两年内搜索到了你在这里发布的同样的问题!
Please also take into account the process that lead to this code duplication!
If you have to change code, it's most of the times faster to duplicate the code than to refactor it so it can be used for your new purpose as well as for your old purpose.
So you have to convince people that refactoring is better than simple duplicating as it saves time in the long run instead of the short term.
Otherwise you find yourself in two years googling to this very same question you posted here!
也许这个 grep 线程 可以帮助你?
Maybe this grep thread can help you?