| 1 | = Extract function = |
| 2 | |
| 3 | A function definition might contain an expression or a sequence of expressions which can be considered as a logical unit, hence a function definition can be created from it. The extracted function is lifted to the module level, and it is parameterized with the free variables of the original expressions: those variables which are bound outside of the expressions, but the value of which is used by the expressions. |
| 4 | |
| 5 | Extracting the new function {{{two_sol}}}: |
| 6 | |
| 7 | {{{ |
| 8 | -module(quadratic). |
| 9 | |
| 10 | -export([f/0]). |
| 11 | |
| 12 | solve(A,B,C) -> |
| 13 | D := B * B - 4 * A * C, |
| 14 | if |
| 15 | D == 0 -> |
| 16 | {-B / 2 / A}; |
| 17 | D > 0 -> |
| 18 | S = math:sqrt(D), |
| 19 | {-(B+S)/2/A, |
| 20 | -(B-S)/2/A}. |
| 21 | D < 0 -> |
| 22 | no_solution |
| 23 | end. |
| 24 | }}} |
| 25 | |
| 26 | Result: |
| 27 | |
| 28 | {{{ |
| 29 | -module(quadratic). |
| 30 | |
| 31 | -export([f/0]). |
| 32 | |
| 33 | solve(A,B,C) -> |
| 34 | D := B * B - 4 * A * C, |
| 35 | if |
| 36 | D == 0 -> |
| 37 | {-B / 2 / A}; |
| 38 | D > 0 -> |
| 39 | two_sol(A, B, D); |
| 40 | D < 0 -> |
| 41 | no_solution |
| 42 | end. |
| 43 | |
| 44 | two_sol(A, B, D) -> |
| 45 | Sqrt = math:sqrt(D), |
| 46 | {-(B+S)/2/A, |
| 47 | -(B-S)/2/A}. |
| 48 | }}} |
| 49 | |
| 50 | == Side conditions == |
| 51 | |
| 52 | * The name of the new function should not conflict with another function, either defined in the same module, or imported from another module (overloading). Furthermore, the name should be a legal function name. |
| 53 | |
| 54 | * If one of the above conditions fails, the transformation starts an interaction to ask for a new function name. |
| 55 | |
| 56 | * The starting and ending positions should delimit a sequence of expressions. |
| 57 | |
| 58 | * Variables with possible binding occurrences in the selected sequence of expressions should not appear outside of the sequence of expressions. |
| 59 | |
| 60 | * The extracted sequence of expressions cannot be part of a guard sequence. |
| 61 | |
| 62 | * The extracted sequence of expressions cannot be part of a pattern. |
| 63 | |
| 64 | * The extracted sequence of expressions cannot be part of macro definition. |
| 65 | |
| 66 | |
| 67 | == Transformation steps and compensations == |
| 68 | |
| 69 | 1. Collect all variables that the selected sequence of expressions depends on. |
| 70 | |
| 71 | 2. Collect variables from the selected variables in step 1, which has binding occurrence out of the selected part of the module. |
| 72 | |
| 73 | 3. Add a new function definition to the current module with a single alternative. The name of the function is an argument to the refactoring. The formal parameter list consists of the variables selected in step 2. |
| 74 | |
| 75 | 4. Replace the selected sequence of expressions with a function call expression, where the name of the function is given as an argument to the refactoring, and the actual parameter list consists of the variables selected in step 2. |
| 76 | |
| 77 | 5. The order of the variables must be the same in steps 3 and 4. |
| 78 | |
| 79 | 6. If the selected expression is a block-expression, eliminate the begin-end keywords from the expression in the body of the created new function. |