Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to analyze and compare logic of similar functions?

I recently picked up a project with a large code base containing much duplication. The problem is that the duplicated functionality was not written by the same people nor copied between them.

So, are there tools that I can use to compare the actual logic of two functions? Here are some of the constraints that would be useful.

  • Ignore order where order is unimportant (such as setting properties)
  • Variable names should be analyzed for similarity, but not required to match if used for the same reasons
  • Look deep into the other methods called by the functions being compared and flatten the logic for comparison

Ideally, the tool would produce a single function output. The actual logical differences would be highlighted in some fashion. It may even be in a form that defines separate functions for the differences in the logic, passing them into the main function which contains the logic that is identical.

This does sound like a tall order, but has anyone come across tools which attempt to do any of these things?

Edit

While there are some cool tools mentioned, it doesn't look like any of them will take the content of called functions into account when comparing the logic of two methods. If I'm incorrect, please let me know!

like image 236
John Fisher Avatar asked Feb 14 '12 16:02

John Fisher


2 Answers

If you download the developer preview of Visual Studio vNext Ultimate, it includes a new Code Clone Detection feature: http://msdn.microsoft.com/en-us/library/hh205279(v=vs.110).aspx

http://blogs.msdn.com/b/zainnab/archive/2011/12/13/visual-studio-11-developer-preview-code-clone-detection-aka-code-clone-analysis.aspx

The download link for the developer preview Visual Studio: http://www.microsoft.com/download/en/details.aspx?id=27543

like image 53
Jared Shaver Avatar answered Oct 23 '22 14:10

Jared Shaver


If you have ReSharper there is a ReSharper plug-in Agent Ralf.

Quote of Agent Ralf's homepage:

In some cases two given methods can be functionally equivalent (same inputs produce the same outputs and side effects) but not textually equivalent. For example, two methods might differ only in the naming of local variables, and are otherwise identical. Agent Ralph can detect this situation, and others like it, and determine that the methods are functionally equivalent.

like image 30
brgerner Avatar answered Oct 23 '22 16:10

brgerner