MT.
  • Home
  • Me
  • Books
  • Projects
Back

Incremental Refactoring

Incremental refactoring is taking small bite size changes and verifying each change along the the way. This is a story of a time during my Pragmint apprenticeship where I did not make incremental changes.

September 22, 2021

2 min read

“It’s red and nothing is working “sigh”. Okay, lets check everything out to the last time the build was green”, this is to paraphrase my mentor at Pragmint.

During my last pairing session, my mentor and I were working on an update to the display of a player’s remaining time view from the Speed Tic Tac Toe game. During this session, we came across a variable that I previously named “time”. He asked, “What was the time measured in, hours? minutes? seconds?”. This was a good question and not obvious by the name. I said, “Milliseconds I think, it’s seconds but with 2 more zeros at the end. In other words, 1000 equals 10 seconds”. Well, as the professionals we are, my mentor saw an issue here. The variable name “time” was not descriptive and even I was having a hard time communicating the true identity. Just image someone in the future having to work with this code. We eventually discovered that the measurement of time was not milliseconds but in fact it was in hundredths of seconds. We renamed the variable.

Now, this blog is not about naming convention, just look at the title, it’s about incremental refactoring. The variable name “time”, or a variation of, was found in many files throughout the codebase, but really they should be named “time in hundredths of seconds”. It made sense to go to each file and update “time” to “time in hundredths of seconds”, and 5–10 minutes later we were finished. Then we ran the tests… and Red red red. We took a couple minutes to try and get the tests to pass, but no luck. Thats when I heard, “It’s red and nothing is working “sigh”. Okay, let’s check everything out to the last time the build was green”. Our scope was too large. At this point, removing all of the changes was the quickest way to get back on track.

The proper way to handle the renaming of all the “time” references would have been to update a single variable at a time, then run the tests. Aka, refactor in increments. When the tests fail, the scope is small and the problem is easy to fix. In addition, the tests stay green. Using incremental refactoring limits the scope of change and ensures that progress is being made, safely, one step at a time.

Based on my experiences from my apprenticeship at Pragmint. https://pragmint.com/