Find Code Duplicates with Sublime Text
Join the DZone community and get the full member experience.
Join For FreeCode 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:Click the screenshots for a bigger view
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.
Sublime Text
Published at DZone with permission of Juri Strumpflohner. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
WireMock: The Ridiculously Easy Way (For Spring Microservices)
-
DevOps Pipeline and Its Essential Tools
-
Testing, Monitoring, and Data Observability: What’s the Difference?
-
What I Learned From Crawling 100+ Websites
Comments