Is there a way to compare the two methods by function rather than cost?

Is there a way to compare if the two methods are equivalent in function (i.e. they do the same) and not equivalent in value (i.e. all the code in the method is the same)?

For example, these two methods are encoded differently, but perform the same function.

public int doIt(int a, int b) {
    a = a + 1;
    b = b + 1;
    return a + b;
}

public int doIt2(int z, int x) {
    int total = z + x + 2;
    return total;
}

I was looking for a way to do this in Eclipse, but I wonder if this is possible even with the trivial method.

+4
source share
3 answers

The only way to be 100% is to prove it mathematically

There are ways:

1- Theorem

2- Model Check

etc.

, , , .

, , , 100% ()

1000 ,

EDIT:

Model Checker, . , , .

https://en.wikipedia.org/wiki/List_of_model_checking_tools

+3

, 2 , .

. , , , , .

: . Enum, . 2 , , .

+1

, , . , , , , .

There are two functions in the OP that need to be compared, doItand doIt2, they can usually return the same answer, given any integer inputs a and b. Unit testing will demonstrate this.

But what if a or b is the largest integer that Java can store MAX_VALUE?

What if there was a side effect from a=a+1?

In these cases, the two functions may seem similar on the surface, but give different results.

+1
source

Source: https://habr.com/ru/post/1670074/


All Articles