Reputation: 2929
Is there a way to scan ruby files for repeating code?
I have almost 30 files each between 200-1000 lines of code and I need to optimize and refactor them.
I was thinking of moving repeated code to a utility class that can be used across all files.
Is there some kind of scanning tool to easily find and mark chunks of repeated or similar code?
Or am I just going to have to bite the proverbial bullet and do it by hand?
Upvotes: 3
Views: 1134
Reputation: 95400
Our CloneDR tool can do this; we are just finishing one up for Ruby. It hunts for duplicate code that matches a set of constraints (on size, # points of variation, similarity of the matching part/total, etc.).
Here are samples of a CloneDR run against most of the Metasploit code base. That's about 460K SLOC of Ruby code; (at least) 14% of the code base is clones.
The first picture below is the summary page. The second is an example detected clone (one of some 3000 found in that code base); you might want to click on this picture in your browser and use the "+" button to make it a bit larger and easier to see.
Upvotes: 2
Reputation: 8646
You can look at flay: https://github.com/seattlerb/flay From the docs:
Flay analyzes code for structural similarities. Differences in literal values, variable, class, method names, whitespace, programming style, braces vs do/end, etc are all ignored. Making this totally rad.
Or, depending on your budget, you can create an account with one of the hosted static code analysis services and push your code there for a checkup.
Upvotes: 7