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?
Consider following examples:
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);
and
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);