It’s easy to end up with a block of code that is overly verbose. We’ve all been there, an initial requirement needed two lines of code to handle some simple logic; a little later another line was added to cover an edge case, and before you know it you have 20 lines of copy pasta.
Finding the time to refactor these blocks of code is nearly always worthwhile and your future self will thank you for it in the long run.
Lets consider the following example data, which mimics a real world dataset I recently encountered.
Some code was written to assign a value (by row) based on the first 3 characters of the value in the code
column.
The code, written in the tidyverse
style, looked something like this;
The code works as intended, but it’s hard to read. If we need to update or add to that code in the future we are more likely to make a mistake, and that mistake is going to be difficult to spot. It also violates the DRY (don’t repeat yourself) principle of programming.
One challenge upon encountering a code block like this is that it may not initially be clear as to how we can improve things, but by taking a systematic approach we can enhance the code in a number of ways.
Let’s try to make this code;
- more readable
- simpler to update and maintain
- more performant
How you prioritise each of the above areas of improvement should be dictated by your specific project requirements, but I would suggest the order above as a good starting point.
Make code more readable
Create functions for repeated logic
So, there’s a lot going on in the original code. Let’s start with the first line of the case_when()
function.
One of the first things you will notice is that there is a lot of repetition. A simple solution to this is to create a function that we can reuse in place of the repeated code.
This function can be used to refactor the first line of the case_when()
function to be much more readable.
We haven’t done anything to improve performance, but we have cut down a lot of the bloat of the original. We can also re-use the meets_criteria()
function elsewhere in our code base if we need to.
The clearer code is also easier to update and any potential mistakes when changing the code should be easier to spot.
Fully utilise the tools available
The case_when()
function allows us to specify a default value and we can see that in the original code a lot of the cases return 'Other'
, so let’s specify 'Other'
as a default value and minimise the evaluations we need to perform.
The refactored code now looks like this.
We now have less code that is more readable AND more performant as we have greatly reduced the number of evaluations that need to be performed. Let’s benchmark our original code with the refactored version.
Benchmarking is a process for measuring the performance of code. It can help to compare different approaches to a problem and allow us to choose the one that runs more quickly or utilises less memory.
We can use the microbenchmark
package to compare our approaches to the original. The usage of the package is as follows;
I haven’t included the code used to benchmark our approaches due to its verbosity.
Lets run our original code and the refactored approach side by side.
The changes have yielded a 5x speed up over the original based on the median execution time. With this in mind I think its fair to say that we fulfilled our aims to make the code more readable, simpler to update and maintain, and more performant.
The approach I took was to refactor the existing methods and cut some unnecessary evaluations, but I didn’t deviate very far from the original approach. Making small incremental changes whilst refactoring can be quick and simple whilst yielding large benefit.
However, if we are prepared to rethink the problem and take a different approach, we might be able to improve things further.
Whilst remaining within the tidyverse, one way to improve things would be to cut down the number of calls to substr()
. We can do this my making 2 temporary columns in our table, containing the substr()
evaluations, which means that substr()
will only be called twice.
The code is still relatively readable, and should perform well. Let’s re-run the benchmarks.
The change in methodology has allowed for us to get a further 1.5x increase in the median execution time of the code. A tidyverse expert could likely extract a little more performance here, but I’m pretty happy with the 7.4x speed up in the median execution time between the original and the refactored code.
But… what if we don’t mind straying from the tidy code approach? When deciding on which frameworks and packages to use, one must consider the current project structure, collaborators, and complexity. For example, importing data.table
to try and speed up this code without utilising it throughout the project isn’t really sensible. However, reverting to base R should always be a consideration.
Harnessing the power of base R
I’m going to start by defining mappings in a nested list structure. This structure closely resembles a list of tuples, though sadly R has no built in tuple object.
Next, I will define a function to map the values in the code column to the lookup object.
We start with 2 calls to substr()
initially to extract the values we will match on into the letter
and number
vectors, similarly to how we made temporary columns in the tidyverse approach.
Next, we create a mapped_value
vector of the same length as the vector that we pass to the code
argument and populate it with the default value “other”.
Finally, we loop through the nested lists of lookups
and set the final values.
To add the values to the data.frame we simply instantiate a new column and assign the output of our function.
Let’s run the benchmarks again.
The base R approach is over 14x faster than the original and 1.9x faster than the tidyverse alternative.
However, whilst we have improved readability and performance with the base R approach over the original, I think the add_mapped_value()
function is relatively complex depending on a persons familiarity with the language, so the tidy approach may win on the readability front.
Next steps
Refactoring code is an excellent way to improve your programming skills. It encourages us to think about why we did something, and how we might do it better.
Start by reviewing code you wrote in the past, ideally something from a few months ago and look for any parts that you find difficult to understand. Could these parts be refactored to be easier to understand and improve performance?
Try to get familiar with the microbenchmark
package as a method for comparing performance.