Wasting Code vs Recycling Code

A few years ago I was working on a medium sized refactor of a framework to make it have better abstractions, be more testable, modular, less confusing etc. It was a framework that around two dozen internal clients were using in fairly diverse ways. Due to some 3rd party API dependencies integration tests were not possible and I was concerned that the refactor wouldn’t support all of our use cases. So as part of rewriting the framework I took the 3 clients with the least in common and rewrote them to use the refactored client.

This took about about two weeks of hacking and it failed

There was just no way the refactored version of the framework could support all the different use cases. In fact the refactored framework had worse abstractions and was less modular than the original. So we stayed with the old framework, but I didn’t delete the branch I had been working on.

A few weeks of ruminating on it and I came up with a better abstraction, that more cleanly fit the problem. The refactored framework seemed like it would work but I still wanted to prove it with those 3 clients. I still had the old branch and I normally commit code every 20-50 lines at a time with a commit message that’s usually a full sentence.

This meant it was easy for me to either patch or in some cases just copy paste from the old branch to the new branch all the changes I had made to the clients.

This cut down the rewrite time from a couple of weeks to a couple of hours.

A couple of years later I started saving ad hoc SQL queries that I was writing. These were queries that I had written just to answer a specific question. They weren’t powering a dashboard or anyone’s KPIs, just things that I wondering about. I never shared any of these queries I just saved them so I would have them for later. Having a bad memory all of the files look like FIND_USERS_WITH_MULTIPLE_ACCOUNTS_IN_LAST_MONTH.sql

Any time I needed to answer a PM’s question or get a feel for something I either had one already written or one that was close enough.

A few months ago while listening to an episode of ToolsDay I was given the idea of source controlling my dot files. My .functions, .alias, .gitconfig etc. These are files that let you configure your tools in Unix-like environments. Then as part of onboarding a new person I was showing them my workflow and they asked what some of my commands were so I sent them the files. They didn’t end up using all of my files but they ended up with something similar.

Throwaway code never gets used again but snippets do.

A few weeks ago I was helping a few friends prep for technical interviews. Last year when I was interviewing I wrote down a study plan to make sure I didn’t miss any topics. I still had the plan when my first friend asked about how I prepped. After 20 minutes of editing it I emailed it to my friend so he could use it. In Gmail you can save this as a canned response. When my second friend asked for the interview plan it only took me a minute to customize the canned response for him.

By source controlling everything for later reference I’m able to reduce the new code I have to write, reuse the code I’ve already written and recycle the code I was going to throw out.