John Fisher
John Fisher

Reputation: 22717

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.

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!

Upvotes: 8

Views: 1257

Answers (4)

brgerner
brgerner

Reputation: 4371

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.

Upvotes: 5

Louis Ricci
Louis Ricci

Reputation: 21106

You could always code your own. This falls under proper unit testing.

IF your similar functions modify some state instance/class, THEN use reflection to test if the property values of the resulting classes are equal.

IF your similar functions modify a database, THEN make a copy of the database run each function on a copy and compare.

But this probably all starts with proper unit tests. If you know all your possible "use cases" then when you find that the output of two (or more) functions is the same for all of these "use cases" you can safely keep one function and throw away the repeats as unnecessary.

Another option is to get actual requirements for what the code/functions are doing. Learning what your system is actually trying to accomplish make refactoring old or repetitious code much easier.


The tools that check for logic duplication will only take you as far as you're willing to work. If you say the current tools don't take into account nested functions or functions that call other functions, then why not refactor the code to inline those called functions so your tool will work? You're just looking for a panacea if you don't even want to refactor method A (which calls methods B, C, D) into method AA which in-lines the code from B, C, D.

IN SHORT with some "work" you can get the current tools to work for you. You may want to contribute to the open source tools to compensate for the failing you mention.

Upvotes: 0

Jared Shaver
Jared Shaver

Reputation: 1339

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

Upvotes: 6

Pongsathon.keng
Pongsathon.keng

Reputation: 1435

There is Duplicate Detection and Consolidation feature on CodeRush. (http://devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/duplicate_code.xml)

This feature detected duplicate code. i am not sure that it is able to detect/compare logic of similar functions.

Hope this help.

Upvotes: 1

Related Questions