Find Code Duplicates with Sublime Text

Code duplication is the worst practice ever, right? Well, actually there might be situations where duplicating can be better than factoring out, but these are very rare. But, before I loose myself in best practices preaches, here's a quick post about a possible way to use Sublime for identifying code duplications.

Often, when I come across some conditions then it happens I see similar code pieces in each of the branches but it's often really hard to identify whether they really match in every part. So instead of going and comparing statement by statement, Sublime has this nice Ctrl+D shortcut which maps to the "Quick Add Next" command. What it does is to select the next match in the current document (btw. also adding multiple cursors, so you could directly edit each of the selections in parallel). So what you can do is to highlight a given code region, press Ctrl+D and look at how many matches there are:
Bad...

The same can obviously be applied by searching in multiple documents. Again, just select the code part of interest and press Ctrl+Shift+F and then click on the "Find" button. This will present you a nice overview of all files with corresponding matches.
Simple but effective.
Kindle

This article has been re-published by


Comments

0

Your ad here?