Before extract, make the code exactly the same

Removing duplication is maybe the simplest and most important fight against code smells there is. And for many cases it is fairly easy if you are using something with reasonable refactoring support like Eclipse. Just mark one instance, extract method/expression, replace second instance with call. Done!

But there is not only duplication, but also near duplication. And I find that, for me, there is much more mental resistance to get to grips with this type. What you should do, and I try (at least for a brief moment), is to make the two sections of the code *exactly* identical first. Then the duplication is just an instance of the above case, and just as easy to fix.

But sometimes I just can’t see how to change them to be exactly the same. I need some glasses. And actually it shouldn’t be so hard to help me out here, but so far I have failed to find that help.

I want to be able to mark two regions in my editor, ask it to mark anything that is different. There are one million diff-tools that works on two (or more!) files, repositories or directories. Why is there no region diff in Eclipse?

Suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.