In “Refactoring, Improving the Design of Existing Code” by Martin Fowler, he presents the Inline Temp refactoring.
Here’s a video walkthrough of the Inline Temp refactoring in Swift and when you might want to use it:
Hey, what’s up everybody? It’s Andy from cleanswifter.com and I’m here to show you another refactoring from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” This week we’re looking at a refactoring called “Inline Temp.” Inline temp is one that once you learn it, you’ll realize that you’ve been using all over for a while. Essentially what inline temp does, is that when you have a temporary variable that’s only ever written to once, you can remove that variable, and move the computation that assigns it a value into every other place it’s read. The benefit of doing this, is that it can actually help you do a larger refactoring. For example, say you had a piece of code you wanted to apply Extract Method to, if you’re using a temporary variable within that code, it might be hard to extract because, what could happen is that the temp variable could be declared many lines above the code that you want to extract, and if you extract that method, the reference to the temp variable will be lost. You’ll have to figure out how to recreate it within the extracted method. What you can easily end up doing though is inline the temp. Take whatever computation that initially populates the value (that is only getting a value written to it once), and then, replace the reference to the temporary variable in the code that you’re trying to extract with a call to the method that provides the initial value. Let’s take a look at some Swift code and see it in action.
Here’s some code that is a pretty simple view controller. We’re overriding
viewDidLoad() and there’s another method in here called
getAppTitle() is fairly simple in that it is just returning a static string. Use your imagination here. Imagine it is calling another class that is doing something more interesting to retrieve the app title. The code that I want to apply this refactoring to is actually up here, in
viewDidLoad(). You can see in
viewDidLoad(), we have a temporary variable created,
titleString, and that is populated from
titleString is then used in two different places. It’s used first to create a label, and then set that string as the text on the label, and add it as a subview on the view.
titleString is then used again later on to be set as the title on the view controller. The first indication here that we can use the inline temp refactoring is that the Swift compiler is prompting us with a warning saying that we have declared this variable as a
var despite it never being mutated, which means never changed. The Swift compiler is suggesting to us that it can be defined as a
let which is a constant. I’ll never be able to write to that again. To show this, if I try to assign a new value to it, now that it is defined as a let, the Swift compiler will tell me I can’t do that. What I’m getting at here is that a variable defined with
let and used as a temp, it’s a good candidate to be ready for inline temp refactoring. The refactoring here is that I’m going to delete this line of code. First I’m going to then take the code that defines the value, and the apply this anywhere else that
titleString is referenced. The temp is gone, and it’s been inlined with the code that referenced it. Now, what I was saying before, why might you want to use this refactoring? Well, it sets you up to then do a greater refactoring, extract method. This code that creates and populates the label could be used in several places and is probably better served as its own method. Now that the temp has been inlined, I can extract the the label creation into a new method,
createTitleLabel(). That’s actually two refactorings in one. First I used Inline Temp, and then followed that up with Extract Method.
Again, inline temp is kind of straightforward. You’ll remove a temporary variable that’s only ever written to once, and then instead put the call to the actual method within each line that references the temp. I hope you liked this video from cleanswifter.com. Check back next week for another video demonstrating a refactoring from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” Thanks.