Replace Temp With Query In Swift

In “Refactoring, Improving the Design of Existing Code” by Martin Fowler, he presents the Replace Temp With Query refactoring.

Here’s a video walkthrough of the Replace Temp With Query refactoring in Swift and when you might want to use it:

Video Transcription

Hey, what’s up everybody, it’s Andy from cleanswifter.com, and I’m back this week to show you another refactoring from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” This week we’re going to take a look at the refactoring called, Replace Temp With Query. Replace Temp With Query is a refactoring that’s a lot like Extract Method, but there’s one difference, it’s a little more specific. With Replace Temp With Query, we’re going to specifically target temporary variables within our code, and extract them into reusable pieces of code. For example, often times within your code you may create a temporary variable that’s then assigned a value based the result of some computation and then reused elsewhere in your code, especially if that value is reused in several different places. The benefit of using Replace Temp With Query is that by replacing the temp with a query, which is then going to be reusable code, you provide a much bigger availability of where that code can be reused from. Additionally, by removing the code that assigns the value to the temp, it will also consolidate the code where the temporary variable is used. Often, temporary variables lead to methods that are longer than they should be. They decrease the readability of code because you then have temporary variables that you have to read and understand where and how that variable is computed.

Let’s take a look at this refactoring in Swift for how we’re going to replace a temp with a query. Here’s a view controller that has three properties on it, two strings which represent a first and last name, and a label that’s going to be set with a name. Here in viewDidLoad() were actually going to compute a full name which represents a first name, a space, and the last name, then store that in a temporary variable called fullName and then set it as the text on the label. The first thing that should jump out at you here is the use of var to define fullName. General Swift best practice is that if a variable is created and only ever assigned to once, it should be defined as let and in fact the compiler actually tells us that here. “Variable was never mutated, please change it to a let.” And the IDE will even help you do that. This plays right into the refactoring, Replace Temp With Query, because by changing this to a let we have the confidence that this variable is never changed anywhere else after it is first assigned. This means fullName always represents this computed value. That sets us up well to do the refactoring. In Martin Fowler’s book, he actually advocates that as part of the refactoring you create a new method to contain the computation for the result. Now I think something a little more Swift like is that we can use computed properties here to actually return this value. The goal of this refactoring is that I want to delete this line of code. To do that, I’m going to create a computed property, fullName. And that’s going to have a getter on it which simply returns this value. Now that I’ve moved where the computation happens, I can continue on my mission to delete the temporary variable’s creation, and then reuse the computed value from the property. You can see a line of code was deleted from viewDidLoad(), no need to look at a temporary variable, and additionally we now have this piece of reusable code where we can access a full name from anywhere else in our code. Now in the case that it’s not so straightforward to use a computed property, you can actually create a method that returns a String (in this case full name) but I think in this case this is a good solution.

There you have it. That’s Replace Temp With Query in Swift. Again, the benefits are that you now have a reusable piece of code which represents that computed value. And additionally, you can delete the temp from the method where it’s being called from, thus consolidating the method and improving the overall readability. I hope you liked this video on this refactoring, Replace Temp With Query from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” You can buy the book through my Amazon Affiliate link in the description for this video. See you next time, and happy cleaning.

3 thoughts on “Replace Temp With Query In Swift”

  1. This is almost nothing, but doing so makes a computation each time the var is accessed, instead of once on creation. Also, if in the meantime (between two calls), firstName or lastName is changed, it gives inconsistent result. Can’t remember if these are var or let.

    1. You’re right, it’s definitely a very small, incremental change, and yes, can introduce another computation. Depending on the computation, may or not be a big deal. Sometimes, you can let readability trump minimizing the number of computations, but certainly measure the performance either way, and if it is problematic, pick a different refactoring. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *