Lately at my new gig, I’ve been working to eliminate duplicate code. As is typical with almost every project I have ever been involved in, my current project has accumulated lots of tech-debt. What starts as a simple cut and paste job for a hot fix and good intentions of fixing shortly afterwards results in countless near-duplicate files that only differ in a few key lines of logic.

I don’t trust myself to be able to simply eye the differences, so I looked into a way that I could get the differences between the two files as git would report it. I found the following solution posted on Stack Overflow:

git diff HEAD:PATH_TO_A_FILE_COMMITTED_TO_GIT  PATH_TO_FILE_TO_COMPARE

I plan on using this approach initially to start eliminating some of the 18 layouts that exist in our project. Many of these layouts are near-identical, differing only in the javascripts and stylesheets they include, as well as potentially an extra conditional snippet of code or a yield statement. I am thinking I can combine the vast majority of these layouts into two or three different layouts. At the same time, I am preparing a gem to be able to handle similarities in layouts in general, and should have it released within the next two months. It will be part of a series of blog posts that I plan to release entitled “Building Reusable UI Components”. Stay tuned.