Articles Javascript

Javascript Refactoring Series: Inline Temp

In this article we take a look at one of the refactoring techniques named as Inline Temp. We will see how to apply these techniques in Javascript code since the original methodology is language agnostic. This is second in the series of articles exploring various refactoring techniques by Martin Fowler.

Inline Temp

You have a temp that is assigned to once with a simple expression, and the temp is getting in the way of other refactorings.

Replace all references to that temp with the expression.


Most of the time Inline Temp is used as part of Replace Temp with Query so the real motivation is there. The only time Inline Temp is used on its own is when you find a temp that is assigned the value of a function call. Often this temp isn’t doing any harm and you can safely leave it there. If the temp is getting in the way of other refactorings, such as Extract Method, it’s time to inline it.


Declare the temp as const if it isn’t already, and compile.

  • This checks that the temp is really only assigned to once.

Find all references to the temp and replace them with the right-hand side of the assignment.

Compile and test after each change.

Remove the declaration and the assignment of the temp.

Compile and test.

 Featured image by:

Gabriel Sanchez