| 1 | = Rename function = |
| 2 | |
| 3 | The name is an important property of a function. While its actual value does not influence the semantics of the program, it is a key point in the readability of the code, and because Erlang programs mainly consist of functions, their names play an often underestimated role in software development and support. It is worth the effort to try to choose them well, and to correct misleading names even when they are user throughout the code. This transformation helps in the latter case: |
| 4 | |
| 5 | {{{ |
| 6 | doit(P) -> |
| 7 | P ! {msg, start}. |
| 8 | |
| 9 | start(Lst) -> |
| 10 | lists:forall( |
| 11 | fun doit/1, Lst). |
| 12 | }}} |
| 13 | |
| 14 | Result: |
| 15 | |
| 16 | {{{ |
| 17 | send_start(P) -> |
| 18 | P ! {msg, start}. |
| 19 | |
| 20 | start(Lst) -> |
| 21 | lists:forall( |
| 22 | fun send_start/1, Lst). |
| 23 | }}} |
| 24 | |
| 25 | == Side conditions == |
| 26 | |
| 27 | * There must be no function with the given name and the same arity as the function to be renamed among the functions in the module, the functions imported in the module, and the auto-imported BIFs. |
| 28 | |
| 29 | * There must be no function with the given name and the same arity as the function to be renamed among the local and imported functions in the modules that import the function to be renamed. |
| 30 | |
| 31 | * If the user does not specify a function to be renamed or the specified function does not exist, the transformation starts an interaction to ask the user to specify one. The user has to select a function from a radio group. |
| 32 | |
| 33 | == Transformation steps and compensations == |
| 34 | |
| 35 | 1. The name label of the function is changed at every branch of the definition to the new one. |
| 36 | |
| 37 | 2. In every static call to the function, the old function name is changed to the new one. |
| 38 | |
| 39 | 3. Every implicit function expression is modified to contain the new function name instead of the old one. |
| 40 | |
| 41 | 4. If the function is exported from the module, the old name is removed from the export list and the new name is put in it. |
| 42 | |
| 43 | 5. If the function is imported in an other module, the import list is changed in that module to contain the new name instead of the old one. |