|Don't ask to ask, just ask|
Re: Equivalency of Codeby Anonymous Monk
|on Feb 07, 2005 at 11:46 UTC||Need Help??|
Does there exist a way to identify functionally similar blocks of codeIn general, you can't, as that will mean solving the halting problem.
That doesn't mean that you will never be able to say "these two pieces of code will do the same thing" - sometimes you can. And sometimes, you will be able to say "these two pieces of code will not do the same thing". It just means that you can't write a program that for any pair of pieces of code can actually decide whether they will do the same thing or not.
As for you example, those pieces of code do not do the same thing. For instance, if $x equals "foo", then $x++ will result in $x becoming "fop", while $x = $x + 1 results in $x becoming 1.
One heuristics that you might try is to compile both pieces of code into bytecode, and compare the bytecodes. That will in a small amount of simple cases tell you that two pieces of code are equivalent.