Skip to content

Advent of Code refactoring, yep that’s also a thing

Posted in Advent of Code, and Experiences

The IntCode problem returned… again

This is the second time I take part in the Advent of Code. Also, it is the first time I actually intend to jingle all the way. Now I face a task which might result in something useful in the next few days, an Advent of Code refactoring. It took me seven days but now I realise this is not the code quick, reap answers, profit kind of contest. I will need to write cleaner code even for this. Even though I am one to enjoy refactoring, I completely skipped it the first few days.

Enters IntCode computer, not once, not twice but thrice in only seven days. Following that pattern, chances are it will return harder and more than once. This seems inevitable. Days five and seven (today), were more painful than they should have. I believe that had I started refactoring right away from day two I could have made it easier on me.

Okay, day two could have been too early but day five should have been a sign. A colleague actually predicted as the fifth day ended that we would get dunked on by day seven with some more IntCode. I was warned and failed to prepare. Next time I will be ready, I would be annoyed if this turned out to be the last of IntCode computers. Even though that would still be a nice little refresher on refactoring especially since I wrote no tests for anything I did so far.

If you want peace, prepare for war

I took upon myself to change my repository structure. If you check it out, you will find a dotnet core project at the root plus some input files. No more, no less. Lazy, simple but effective so far. I know that preparing for this event in November would have saved me loads of time. I am paying my debt now by moving the main problem-solving classes and input files down into a project folder.

Next, I created a test project with a base class. Turns out that neat little boilerplate I wrote for reading input files and running challenges codes helped. This reflected the structure for my tests. Once done, I put together a small abstract test class with the main test method. One I could reuse for each day test file by only passing different parameters. It felt like the right time to experiment with xUnit which I surprisingly never used.

I really like how you can skip repetitive code by just putting a bunch of parameters above your method. Also, the use of expression bodies C# offers gives you beautiful code such as this:

Isn’t it beautiful? I used carbon.now.sh also a first time.

Adding more tests

Once I wrote that test validating my day seven epic I could go into the chopping and mutating. I thought I could go on and add tests for days two and five which also require the use of that same IntCode computer. So far, I copy-pasted the whole thing from file to file as I thought I would not see it ever again. Bad JD, bad! Now I actually intend to extract the class into its own thing and reuse it for the past days’ appearances thus ensuring I don’t break it. The good thing though is that I kinda implemented that today. With more foresight, this could have been a ten minutes challenge instead of two hours had I refactored after day five.

Yes, so the tests. Once I wrote the tests covering day seven, I moved the class to its own file. Then, I made sure these still passed. From there I moved on to writing a few tests matching past scenarios into a new class IntCodeComputerTests. I made sure these passed as well and changed the implementation of day five to use it. For day two I would need to add a way to set the verb and noun to my messy implementation. Took a couple of minutes to do. I added the noun and verb setting and wrote a test to validate that. Once green I changed my day two implementation to match.

An Advent of Code refactoring

Now that I extracted and tested my IntCode computer class, I can finally start my first and hopefully this last Advent of Code refactoring of 2019. For some reason, I created some dumb stuff that made my life only harder. Maybe it even made me slower to solve the day five problem. I had to inline some methods that I each used only once. Beginner move. Then I did some general cleanup, added some utility methods that I reused around that IntCode stuff.

Eventually, I expanded upon the day seven meat which I won’t spoil here but feel free to check out the repo. Considering it already is midnight and technically December 8th I thought I’d just wrap up the refactoring and this post. Yeah, day 8 won’t begin before 5 am here in London I would like a break and some sleep. Let’s just push the code, finish that margarita and go to bed.

Can’t wait to see what the next challenge will be. Until then, peace.

Be First to Comment

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    %d bloggers like this: