Version 2 (modified by manualwiki, 13 years ago) (diff) |
---|
Reorder function parameters
The order of a function’s arguments is a small, aesthetic aspect of a program. Swapping arguments can improve the readability of the program, and it can be used as a preparation for another refactoring, eg. to create a tuple from arguments that aren’t next to each other.
The idea is illustrated by the following simple example, where a function’s three arguments are reversed. To maintain the meaning of the program, every call of the function must be modified: the order of expressions that provide actual parameters must be reversed too.
Simple argument reordering:
sum(A,B,C) -> A+B+C. caller() -> sum(1, 2, 3).
Result:
sum(C,B,A) -> A+B+C. caller() -> sum(3, 2, 1).
This refactoring can be carried out in almost every case without any problems, only dynamic function calls put limits to its applicability. Within the bounds of static analysis, every parameter reordering can be compensated at the place of function calls.
Side conditions
- When a function application has an argument with side effects, the transformation may only be carried out after a warning that the order of side effects most probably will change, which may change the way the program works.
- When the given order is not legal, meaning it does not contain all of argument indices, the transformation starts an interaction to ask for a new order.
Transformation steps and compensations
- Change the order of patterns in every clause’s parameter list in the function according to the given new order.
- For every static call of the function, change the order of the expressions that provide the actual parameters to the call according to the given order (obviously in all modules).
- Every implicit function expression is expanded to the corresponding explicit expression which contains a static call to the function; this function call is then updated as described in the previous case.
- For every call of the function that provides the arguments as a list, insert a compensating function expression that changes the order of the elements in the list according to the given new order.