Flay gem - Write clean code
This tutorial is regarding Flay gem helps you to avoid duplications and similarities in code to write a clean code
Flay gem analyzes your code and returns the scope to refactor
installation process :-
gem install flay
or add below line in you Gemfile and bundle
gem ‘flay’
after successfully installing the gem below command will give you output
flay ./app
this command will process all the code and return you the result and score (lower is better) suppose you have to run it for only controllers or models then you can specify it in the path in front of the flay
flay ./app/models
flay ./app/controllers
Output:-
The output will be like
1
2
3
4
5
6
7
8
9
Total score (lower is better) = 277
1) IDENTICAL code found in :defn (mass*2 = 64)
./app/something.rb:71
./app/somethin2.rb:79
2) Similar code found in:attrasgn (mass = 63)
./app/something.rb:71
./app/somethin2.rb:79
./app/somethin3.rb:89
So, above output tells that files have identical or similar code Then on this finding, you can try to optimize the code of that particular file by using SOLID or DRY principles
Flay also have some extension of JS and ERB
- flay-actionpackUse Rails ERB handler.
- flay-jsProcess JavaScript files.