Toll free (US) : +1 (888) 838-3532 | NYC: +1 (646) 491-6866

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

Command to see output

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

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.

rails

footer-icon

Pin It on Pinterest

Share This