Tests, not Documentation helps the future self

Today, I had to do a couple of code fixes to an old project that I did as a freelancer. I was returning to this code base after 2 months :

The app has a UI where users get to select 3 out of 5 options. There is a sampling logic thats here but its not important for this post. The client requirement was to simply change the total number of options given to the user from 5 to 10. I simply made the change and started testing the app. I noticed that it worked as expected most of the time but occasionally it broke. This annoyed me a bit because I thought it would be a small fix and I only accounted for 5-10 minutes to do this. Dismayed, I started reading my old code to understand my old sampling logic code, as I was reading, I already started anticipating the time sink it is going to be to debug this UI code - Should I make changes and do the userflows again and again to test it out? Should I use console.log or setup a debugger.

Just as I was doing it, I encountered this old code block that I had forgotten I'd written when I first wrote the sampling logic.

function testSampling() { let structureSet = createMapOfCategoryAndItems(structureMap); let structureChoicesToGiveUser = sample2Choices(structureSet, structureMap); let randomizedStructureChoices = getRandom(structureChoicesToGiveUser, 5); console.log("STRUCTURE CHOICES:", randomizedStructureChoices); console.log( "STRUCTURE MAPPED VALUES:", randomizedStructureChoices.map((choice) => structureMap[choice]) ); }

I have never felt more thankful to a former self. Debugging this became so much easier now, first of all now all the code relevant to the sampling feature is right here, with none of the UI or user flow related things. I can just change the number 5 there and run this function with a single command multiple times to verify where it breaks and why it breaks. Most importantly, I don't have to recall details of every constituent function that goes into this sampling feature and just focus on the broken function.

This project doesnt have a test suite or any tests but I remember that it took me a while to implement the sampling logic that the client wants and I had thought that this seems like something that might be tweaked and so let me write a test for that. One of those instances where I genuinely thanked my former self.

Increasingly as I work as a developer on multiple long running independent software projects with intertwining timelines, I find tests to be a lot more important to keep me productive. They provide a quick way to get back into a codebase even with multiple context switches.