wiki:RefactoringSteps/MergeExpressions

Version 1 (modified by manualwiki, 13 years ago) (diff)

--

Merge expressions

During the merge expressions transformation, a new match expression is created that binds the selected expression to the variable that the user has given as input, and all instances of the expression is changed to the variable.

Example of Merge Expressions transformation:

foo(A,B) ->

   peer ! {note, A+B},
   A+B.

Result:

foo(A,B) ->
   V = A+B,
   peer ! {note, V},
   V.

Merge expression duplicates refactoring is executed in the function, A+B expression in the Message-Passing (peer ! note,A,B) is stored in V variable, then all instances of the expression are changed to V variable.

Side conditions

  • The expression cannot be substituted if any of its sub-expressions have side effects.

  • The transformation cannot be executed if the expression is in the head of a list comprehension, in a pattern or in a guard expression.
  • If the expression occurs in a generator expression, it should not contain variables that are bound by generator patterns.
  • The given variable name should not already exist in the given scope in order to avoid name clashes.
  • If the given variable name is not legal then the transformation starts an interaction to ask for a new variable name.

Transformation steps and compensations

  1. Determine the insertion point. The insertion point is the first possible location within a body where all of the variables of the expression have already received their binding. If the selected expression contains no variables, the insertion point may be in any containing scope; currently the innermost scope is chosen. If the selected expression contains at least one variable, the insertion point is in the outermost scope that contains all variables.
  1. Insert a new match expression to the insertion point that binds the expression to the new variable. If there is an expression to be replaced at this position, the match expression should replace it.
  1. Substitute all other instances of the expression to the new variable. Note that not all expressions whose structure is identical to the expression are instances of the expression: all variables have to have the same binding, and therefore the same scope. These substitutions should remove surrounding parentheses from instances.