In “Refactoring, Improving the Design of Existing Code” by Martin Fowler, he presents the Move Method refactoring.
Here’s a video walkthrough of the Move Method refactoring in Swift and when you might want to use it:
Video Transcript:
Hey, what’s up everybody, it’s Andy the Clean Swifter and I’m back here to show you another refactoring from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” Each week we’re taking a look at a different refactoring from that book and how to implement it in Swift 3. This week we’re going to talk about the Move Field refactoring. Now the Move Field refactoring you’re going to see, and I bet you’re going to write it off as something so trivial that you’ll be wondering why I’m even making a video about it. Before you do that, I want you to realize that the essence of refactoring is identifying an inefficiency with regard to the structure of your code, and improving it. It’s the building blocks of being able to look for these, knowing what to do, and building up that body of experience about refactoring and being able to apply that in much larger puzzles after you’re put together these smaller pieces and fundamentals along the way. Move Field is just that. It’s a fundamental refactoring where you’re going to see that in one class, or struct, that one field exists and it might not be the best place for it. You’re going to take that field and find a better place for it, and you’re going to move it there. All references will then point to that new field in the new location. Remember, the point is you’re taking a field from an inappropriate place to a better place. You’re not always going to be able to identify that up front. Don’t try to over optimize your code, whether it’s from a performance standpoint, or a refactoring standpoint. Go with your first natural design, and then once you get there, reevaluate it, and look for that better place of what the code could be. In this case we’re going to look at it from the perspective of the Move Field refactoring.
Just this week I’m going back to revisit some code in an app I worked on over a year ago. We went to long extents to refactor a NSFetchedResultsController
from a UIViewController
from it’s tableView datasource
and delegate
. They are all separate classes in separate files in Objective-C. Going back to that code, it’s hard to digest and understand. At the time, we thought we were being so clever with this refactoring, and thought the code was going to be so flexible for further design. Little did we know, it would be the last update we’d make to that code for a year and a half. We’re actually going back to it now, and rather than being flexible for design, it’s kind of hard to improve. My moral here with these basic refactorings, remember, yes they are basic, but they’re the fundamental for doing something bigger, and being able to put the puzzle pieces together to ultimately improve your code. Let’s look at the Move Field refactoring.
We’re going to reuse the sample code that we used in our refactoring last week, Move Method, and continue with that. I’ve added a new method here, displayFirstName()
that will help explain this example for Move Field. In general, there’s a struct that represents a person’s name. It has a first and last String
. There’s a view controller with a label, a name, and nickname String
. There’s a viewDidLoad()
implementation, and another method to displayFirstName()
and lower on is a separate class for NameLabel
. For the Move Field refactoring, what we’re going to do is identify a field in a class and find a new place for that field that makes more sense. Let’s take a look at this view controller to identify that field that’s going to move. viewDidLoad()
sets a name label based on the first name. There’s this other method which isn’t being called at the moment, which is displayFirstName()
. displayFirstName()
looks like it is doing something a little different than setNameLabel
. displayFirstName()
is actually going to use nickname
, the String
on this view controller, and then based on whether there is a nickname or not, format the string and set it on the label. The first thing that jumps out to me is that nickname
is being used here as a variable separate from the Name
object. That to me smells a little bit. I think the nickname
would be more appropriate as an attribute on the Name
struct. This is where we are going to start honing in on our Move Field refactoring. Move Field refactoring now that I’ve picked nickname
as something that doesn’t make sense in this view controller, and would make better sense in the Name
struct, I’m simply going to pull this out of the view controller, and insert it into the Name
struct. And now that the nickname
is there I’m going to get a compiler error here back in the view controller because nickname
is no longer a property on the view controller, and instead it’s being provided by the Name
. Then I can access it through name
and if name
has a nickname
then set it to be this interesting label where it’s the firstName
plus the nickname
, or just set the label to be the first name. That’s it! That’s Move Field in a nutshell. Depending on which logic might be used for computing nickname
on setting or getting, you may want to consider different types of encapsulation for the logic which might happen at that point. None of that is happening here. In essence, Move Field is removing a property from one class, and adding it to another, in this case a struct. In my opinion, this is a much cleaner implementation. nickname
goes with a Name
. It’s no longer a property hanging off the view controller. It’s well enapsulated within Name
.
There you have it, it’s the Move Field refactoring from Martin Fowler’s book, “Refactoring, Improving the Design of Existing Code.” It is a very simple refactoring, and you saw a very simple implementation of it where you move one field from a class to another class, all in the sake of finding a better home for that field which can make more logical sense for your code, and support a better encapsulation of your code. I hope you enjoyed this video. You can buy Martin Fowler’s book. “Refactoring, Improving the Design of Existing Code” in the Amazon Affiliate link in the description for this video. I hope you tune in next week for another refactoring. In the meantime, happy cleaning.