Code Refactoring Gem – Flay

Keeping your code elegant and no code clones while building up a vast Rails application can be a significant test, notwithstanding for an accomplished developer. Fortunately, there is a gem that make this code refactoring much simpler. For a great many people, dead/wasteful code will develop after some time. Others will find themselves in such a situation when they take over legacy projects.On the off chance that you need to keep your code maintainable and optimized, take a look at a gem that have been doing the job for me.

Flay examines code for structural likenesses. Differences in literal values, variable, class, method names, whitespace, programming style, braces vs do/end, props versus do/end, and so forth are all overlooked,making this absolutely rad. It’s fit for recognizing both correct and close matches, and does in certainty discover a few spots in Active Record where patterns repeat. In its current state, Flay’s output is very primitive: a list of repeated code nodes, together with a weight to rank them by and line numbers and file names where the repeated code appears. Code that flay reports as comparative is a decent possibility tool for refactoring.

Command to install

sudo gem install flay

Command to see output

Cd “Path to your project folder”
flay “path of the folder”
Eg : flay ./app/controllers - Identifies the code duplication in all the controllers.
flay ./app - Identifies the code duplication in entire project
flay ./app/controllers/example_controler.rb - Identifies the code duplication in specified controller.

Example of Output
An output is generated of the code duplicated areas like this:

sridevi@carmatec-MS-7788$ flay ./app/models/*.rb
Total score (lower is better) = 1666

1) Similar code found in :call (mass = 170)
./app/models/example.rb:8
./app/models/example.rb:9
./app/models/example.rb:10
./app/models/example.rb:11
./app/models/example.rb:15
./app/models/example.rb:17
./app/models/example.rb:19
./app/models/example.rb:20
./app/models/example.rb:22
./app/models/example.rb:23

2) Similar code found in :defs (mass = 154)
./app/models/example.rb:260
./app/models/result.rb:195

3) Similar code found in :defs (mass = 138)
./app/models/feedback.rb:62
./app/models/example.rb:54
./app/models/result.rb:51

4) Similar code found in :call (mass = 136)
./app/models/example.rb:12
./app/models/example.rb:13
./app/models/example.rb:14
./app/models/example.rb:16
./app/models/example.rb:18
./app/models/example.rb:21
./app/models/example.rb:24
./app/models/example.rb:25

5) IDENTICAL code found in :defn (mass*2 = 128)
./app/models/result.rb:7
./app/models/summary_report.rb:7

6) IDENTICAL code found in :defn (mass*2 = 120)
./app/models/example.rb:17
./app/models/example.rb:23

The total app score of 1666 (‘lower is better’ advice holds true) can be viewed in its individual components showing areas that provide the most bang for the buck. For experienced developers operating on their own or in a small team Flay may be unnecessary. However, on larger projects (as the one I ran it on) or those with beginner or intermediate programmers it can help increase the maintainability of your codebase.

Below is the pictorial representation of how to refactor the code in rails app. For example, If the same code duplicate in many controllers keep it in ApplicationController and call the method by using filters.

Related Posts

Leave a Comment

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

en_USEnglish