One easy smell to identify in your code is “Long Method.” Methods that are longer than 10 lines are generally viewed as potential problem areas and can harm the readability and maintainability of your code. In this post, I’m going to demonstrate cleaning up this code smell with some simple refactoring!
Here’s a segment of code guilty of “Long Method.”
We want to improve the readability of this code by refactoring it. As I mentioned above, we’re going to try to decompose this method into smaller, more readable methods by using the “Extract Method” refactor.
Before we start, it’s a good idea to identify the behavior of our method so that we can ensure that the code behaves the same after our refactor. It can often be tempting to add new functionality or to change how the code works but this can cause errors and should be resisted if possible. Let’s take a closer look at what our method is doing.
#setup! method is building up a hash of changes to apply to our model and then updates all of them at once using mass-assignment. The attributes and values to update are stored in the variable
It’s also worth noting the arguments and return value of the method. So that I can contain this refactor to only this class, I want to make sure that all of the blackbox interfaces remain the same. In this case, it means that this method should continue to be called with no arguments and return the saved instance of the model.
With all this in mind, let’s get started! The first step is to identify segments of the code I want to extract and to create new methods from them. Luckily for me in this situation, our sections seem pretty well delimited by comments! A comment that explains a section of code is often an indicator of a block of code that we can extract into its own method. Let’s take this first section as an example.
This section does two things: creates the
invoice_attrs variable and sets general attributes for our invoice. We can extract this into a method that does the same thing. I’ll also take this opportunity to remove redundant calls to
We can now call this method directly from our
That’s the basic gist of this! I’m going to apply the same “Extract Method” pattern a few more times and we’ll fast forward to the end result.
That’s a lot better! Now we can clearly see that
#setup! is a procedural method that will assign a bunch of attributes and return
There’s just one small tweak I’d like to do to finish this off. One of the key aspects of this method is that it updates all of the attributes in a single transaction, but there’s a simpler way to accomplish the same thing without creating a temporary hash. As this is an ActiveRecord model, we can simply assign the changes directly to the model instance and then
#save! the instance which will have the same result!
Refactoring can really help with code maintainability and should be practiced regularly! To learn more about refactoring, we recommend picking up a copy of the Refactoring book. Stay tuned for the next episode and more tips!