wiki:RefactoringSteps/EliminateVariable

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

--

Eliminate variable

In this refactoring, all instances of a variable are replaced with its bound value. Those instances of the variable where the value of the instance is not used can be dropped.

Replacing all instances of variable Y with its value:

func(X) ->
   Y=X+2,
   Pid ! {value,Y},
   Y.

Result:

func(X) ->

   Pid ! {value,X+2},
   X+2.

Side conditions

  • The variable has exactly one binding occurrence on the left hand side of a pattern matching expression, and not a part of a compound pattern.

  • The expression bound to the variable has no side effects.
  • Every variable of the expression is visible (that is, not shadowed) at every occurrence of the variable to be eliminated.
  • If the selection is not specify a variable but it is inside a function clause, the tool gives a list to the user to select a variable. The list contains the reachable variables in the given function clause.

Transformation steps and compensations

  1. Every occurrence of the variable is substituted with the expression bound to it at its binding occurrence, with parentheses around the.
  1. If the result of the match expression that binds the variable is discarded, the whole match expression is removed. Otherwise, the match expression is replaced with its right hand side.