Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Code Refactoring Gem – Flay

ruby gems flay gem Code Refactoring Gem – Flay ruby on rails

  • Please log in to reply
No replies to this topic

#1 rubyonrailsdevelopment

rubyonrailsdevelopment

    Signalman

  • Members
  • 23 posts
  • LocationUS California

Posted 02 December 2016 - 09:00 AM

Code-refactoring-tool-Flay-gem-.jpg

 

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
1
sudo gem install flay

Command to see output

Cd “Path to your project folder”
1
Cd Path to your project folder
flay “path of the folder”
1
flay path of the folder
Eg : flay ./app/controllers - Identifies the code duplication in all the controllers.
1
Eg : flay ./app/controllers - Identifies the code duplication in all the controllers.
flay ./app - Identifies the code duplication in entire project
1
flay ./app - Identifies the code duplication in entire project
flay ./app/controllers/example_controler.rb - Identifies the code duplication in specified controller.
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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.

 

Read More From here : http://www.railscarm...oring-gem-flay/

 

Ruby on Rails Web Development Company







Also tagged with one or more of these keywords: ruby gems, flay gem, Code Refactoring Gem – Flay, ruby on rails

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users