Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Find Code Duplicates with Sublime Text

1 min read

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:

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.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus