Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find a similar code fragment?
    primarykey
    data
    text
    <p>Does anyone has some tool or some recommended practice how to find a piece of code which is similar to some other code?</p> <p>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.</p> <p>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?</p> <p>Consider following examples:</p> <pre><code> float xDistance = 0, zDistance = 0; if (camPos.X()&lt;xgMin) xDistance = xgMin-camPos.X(); if (camPos.X()&gt;xgMax) xDistance = camPos.X()-xgMax; if (camPos.Z()&lt;zgMin) zDistance = zgMin-camPos.Z(); if (camPos.Z()&gt;zgMax) zDistance = camPos.Z()-zgMax; float dist = sqrt(xDistance*xDistance+zDistance*zDistance); </code></pre> <p>and</p> <pre><code> float distX = 0, distZ = 0; if (cPos.X()&lt;xgMin) distX = xgMin-cPos.X(); if (cPos.X()&gt;xgMax) distX = cPos.X()-xgMax; if (cPos.Z()&lt;zgMin) distZ = zgMin-cPos.Z(); if (cPos.Z()&gt;zgMax) distZ = cPos.Z()-zgMax; float dist = sqrt(distX*distX +distZ*distZ); </code></pre> <hr> <p>It seems to me this has been already asked and answered several times:</p> <p><a href="https://stackoverflow.com/questions/204177/what-tool-to-find-code-duplicates-in-c-projects">https://stackoverflow.com/questions/204177/what-tool-to-find-code-duplicates-in-c-projects</a></p> <p><a href="https://stackoverflow.com/questions/191614/how-to-detect-code-duplication-during-development">How to detect code duplication during development?</a></p> <p>I suggest closing as duplicate here.</p> <hr> <p>Actually I think it is a more general search problem, like: How do I search if the question was already asked on StackOverflow?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload