| | 1 | = Duplicate Code Elimination = |
| | 2 | |
| | 3 | The “ Duplicate Code Elimination” transformation eliminates a duplicate code group. This transformation needs a duplicate code group found with the filtered_suffix_tree algorithm, with max_invalid_seq length parameter set to 0. If the members of the group are found in different modules the transformation asks the user to enter a module name where the extracted function should be placed, and exported. Every occurrence of the group will be replaced with a function call to the extracted function. The transformation asks the user to enter the extracted function's name. |
| | 4 | |
| | 5 | |
| | 6 | |
| | 7 | Eliminate a clone gorup: |
| | 8 | |
| | 9 | {{{ |
| | 10 | -module(clone). |
| | 11 | |
| | 12 | -export([f/0, g/0]). |
| | 13 | |
| | 14 | f() -> |
| | 15 | A = 5, |
| | 16 | B = A + A, |
| | 17 | case A of |
| | 18 | 5 -> io:format("~p~n",[A]); |
| | 19 | _ -> io:format("Function f") |
| | 20 | end. |
| | 21 | |
| | 22 | g() -> |
| | 23 | C = 6, |
| | 24 | case C of |
| | 25 | 6 -> io:format("~p~n",[C]); |
| | 26 | _ -> io:format("Function g") |
| | 27 | end. |
| | 28 | }}} |
| | 29 | |
| | 30 | Result: |
| | 31 | |
| | 32 | {{{ |
| | 33 | -module(clone). |
| | 34 | |
| | 35 | -export([f/0, g/0]). |
| | 36 | |
| | 37 | f() -> |
| | 38 | A = 5, |
| | 39 | B = A + A, |
| | 40 | new_fun(A, 5, "Function f"). |
| | 41 | |
| | 42 | g() -> |
| | 43 | C = 6, |
| | 44 | new_fun(C, 6, "Function g"). |
| | 45 | |
| | 46 | new_fun(NewVar1, NewVar2, NewVar3) -> |
| | 47 | case NewVar1 of |
| | 48 | NewVar2 -> io:format("~p~n",[NewVar1]); |
| | 49 | _ -> io:format(NewVar3) |
| | 50 | end. |
| | 51 | }}} |
| | 52 | |
| | 53 | == Side conditions == |
| | 54 | |
| | 55 | * The extracted function name should not conflict with an already used function name. |
| | 56 | |
| | 57 | |
| | 58 | == Transformation steps and compensations == |
| | 59 | |
| | 60 | 1. Given the clone name, clone group, the transformation analyses the group and finds the variables which will be extracted. |
| | 61 | 2. Calls the extract_funcion transformation to create a new function, and replaces the group members to a call to the extracted function. |