如何找到类似的代码片段? [英] How to find a similar code fragment?

查看:108
本文介绍了如何找到类似的代码片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能找到一些类似于其他代码的工具或推荐实践吗?

Does anyone has some tool or some recommended practice how to find a piece of code which is similar to some other code?

通常我写一个函数或代码片段,记得我以前已经写过类似的东西,并且我想重用以前的实现,但是使用纯文本搜索不会显示任何内容,因为我没有使用变量名称完全相同.

Often I write a function or a code fragment and I remember I have already written something like that before, and I would like to reuse previous implementation, however using plain text search does not reveal anything, as I did not use the variable names which would be exactly the same.

具有类似的代码片段会导致不必要的代码重复,但是,如果代码基数较大,则不可能将所有代码都保留在内存中.是否有任何工具可以对功能上与相似"的代码和标记的片段或功能进行一些分析?

Having similar code fragments leads to unnecessary code duplication, however with a large code base it is impossible to keep all code in memory. Are there any tools which would perform some analysis of the code and marked fragments or functions which are "similar" in terms of functionality?

请考虑以下示例:

  float xDistance = 0, zDistance = 0;
  if (camPos.X()<xgMin) xDistance = xgMin-camPos.X();
  if (camPos.X()>xgMax) xDistance = camPos.X()-xgMax;
  if (camPos.Z()<zgMin) zDistance = zgMin-camPos.Z();
  if (camPos.Z()>zgMax) zDistance = camPos.Z()-zgMax;
  float dist = sqrt(xDistance*xDistance+zDistance*zDistance);

  float distX = 0, distZ = 0;
  if (cPos.X()<xgMin) distX = xgMin-cPos.X();
  if (cPos.X()>xgMax) distX = cPos.X()-xgMax;
  if (cPos.Z()<zgMin) distZ = zgMin-cPos.Z();
  if (cPos.Z()>zgMax) distZ = cPos.Z()-zgMax;
  float dist = sqrt(distX*distX +distZ*distZ);


在我看来,这已经被问过好几次了:


It seems to me this has been already asked and answered several times:

https://stackoverflow.com/questions/204177 /what-tool-to-to-find-code-duplicates-in-c-projects

如何在开发过程中检测代码重复?

我建议在此处关闭重复项.

I suggest closing as duplicate here.

实际上,我认为这是一个比较笼统的搜索问题,例如:如何搜索是否已经在StackOverflow上问过该问题?

Actually I think it is a more general search problem, like: How do I search if the question was already asked on StackOverflow?

推荐答案

您可以使用 Simian .它是一种可检测Java,C#,C ++,XML等(甚至是纯txt文件)中重复代码的工具.它甚至可以很好地集成到CruiseControl之类的工具中.

You can use Simian. It is a tool that detects duplicate code in Java, C#, C++, XML, and many more (even plain txt files). It even integrates nicely in a tool like CruiseControl.

这篇关于如何找到类似的代码片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆