During last year’s Advent of Code, upon IntCode returning for the third time after completing day 8, I refactored my day 7 code. The idea then was to make sure I don’t waste time copy/pasting code while risking a functionality loss. By the seventh day, our IntCode computer reached a state where it neared completion. After that, I think that for most days we could just use it and solve problems more or less easily depending on how buggy the original code was. I think I fixed one or two bugs later on after that, I relied on it all the way. Welcome to my second 2020 Advent of Code refactoring!
This year, I may have prematurely refactored a class I built thinking it would become the next IntCode-like thread. It was for that assembly-bootleg on day 8. I felt pretty proud of that clean code I looked forward to reusing it. Eight days passed so far and I am still to use that refactored code, so why try again? While my day 8 refactoring was baseless, now I’m going to extract into some sort of utility package the methods that keep reappearing. It’s mostly parsing methods, stuff like convert string or list of strings into a collection of integers and such.
If I learned anything since my first lines of code, it is that copying and pasting code is wasting time. However, we want to save time and have multiple valid reasons to do so. First, so that we can have enough time before work to do things like working out or watching Netflix. Second, well it’s always nice to revisit code to figure ways to make it cleaner. You don’t need a third but if you really want it, you can climb that private leaderboard. You know, to claim bragging rights or touch the stars of the global leaderboard.
Now the process, I started all the way back from day 1. From there, I worked my way through all days until today. I and used my memory on what method reappeared on later days and voila. Per example, as soon as day 2 I found my first method. I didn’t need to refactor it but I could extract it. At one point, I have a method which checks that a value is within a certain numeric range. Today, I created range struct with a method to validate a value is within bounds. After extracting that struct and renaming it, I put it into that new library and reused it in my day 2 code.
It’s a simple bit of code but when written or pasted a 5 am you can be sure that mistakes will happen. These can drag you for hours especially when located in methods you deem too easy to implement to be wrong. Also, as I go, I add unit tests to make sure I don’t miss anything.
Some methods required minor adjustments, per example there was one where I passed the whole input (an array of strings) but then only used the first value. In that case, I would change the method to only pass the first line of my input. A minor change yes, but it allows me to reuse that code for two separate days.
In total, I spent about an hour between taking notes for this post and extracting methods from existing patterns and methods. Eventually, I got three methods reused between days 2, 4, 9, 10, 11, 15 and 16. Even though my previous refactoring didn’t bear any fruits (yet), I do hope this one does. How unlucky do I need to be so that none of these methods shows any use whatsoever? Only time will tell, I need to push this code and enjoy the afternoon holiday I accidentally booked for today.
Thank you for reading yet another of my Advent of Code 2020 posts, I will see you tomorrow, maybe. In the meantime, you can check out my Go Cloud series about building and deploying a Golang app to AWS. Also, I will push my code on Github at CodingNagger/advent-of-code-2020 so feel free to check it out so that you can see all my refactoring. Bye!
Photo by RF._.studio from Pexels