Excel.
I'd guess that 90% of my refactoring consists of replacing duplicated code with method calls. Unfortunately, there's no tool that I know of to do this automatically (though there are plenty of tools to extract methods and such). And it's hard for the human eye to spot tiny differences in code that's mostly the same.
So I use an Excel template which can compare from two to six sets of code. I use one worksheet for each number of duplicates. In the one for three sets of code, for instance, columns A through C are blank. Column D contains this:
=IF(AND(A1=B1,B1=C1),"=","***")
=IF(AND(A2=B2,B2=C2),"=","***")
=IF(AND(A3=B3,B3=C3),"=","***")
etc. for a thousand lines or so down.
When I have code that appears to be duplicated, I copy each set of code and paste it in parallel columns in my spreadsheet. The lines that really are the same show up as =
. The lines that are different show up as ***
.
This keeps me from introducing bugs by replacing code that isn't really duplicated. I can then go back and focus on the lines that have differences, often by replacing literals with parameters. (For instance, if one set of code has int myNumber = 5;
and another set has int myNumber = 6;
, I pull the 5 and 6 out into a parameter so the line ends up as int myNumber = myParam;
in both cases.)
After a bit of work, I paste the code sets again and compare. Through this iteration, eventually I end up with =
all the way down, at which point the code is genuinely duplicated, and I can replace all instances of it with an extracted method.