lmkk
lmkk

Reputation: 609

Refactor ruby on rails

What is best way to refactor in Ruby on Rails. I have a project where i would like to refactor several of my objects - extra properties and renaming of existing properties. How do i do this this the easiest way?? I created the different objects with scaffolding "rails generate scaffold foo name:string...." I do start from scratch or is there some cool ruby command, like "rails refactor foo name:string"..

Upvotes: 0

Views: 606

Answers (4)

Bijendra
Bijendra

Reputation: 10053

Firstly a good knowledge of your application and then start in a procedure and serialize it.

  • Use helpers to optimize.Methods should be small enough and minimize the possiblity of repetition of codes.
  • Use application files which helps a lot
  • You can also write methods in private to reduce the lines in methods.
  • Alwayz be sure where refactoring is done and its dependencies..

Thanx

.

Upvotes: 0

fl00r
fl00r

Reputation: 83680

You should learn some Rails best practices and use them to refactor your code

Upvotes: 1

Paul Russell
Paul Russell

Reputation: 4747

As far as I am aware, there is no automated way to do this. The key things as far as I'm concerned is to make sure that your test coverage is good in the relevant area (and preferably across your whole app) before you start.

Once that's done, I've tended to apply fairly brute force tactics to do that actual refactor:

  • Run all tests.
  • Searching for relevant file names and strings within files, making a value judgement as to whether you're changing the right thing (don't just do search/replace).
  • Refactor your tests along side the code itself.
  • Run all tests again.
  • Repeat until it works.

The reason that refactoring isn't very automated in Ruby (in my view at least) is because the language is so dynamic, so it's very hard for any automated tools to make sure they've covered all the bases.

Upvotes: 2

Kieran Senior
Kieran Senior

Reputation: 18220

Refactoring is based on human perception. Sure, omission of some redundant things could be figured out by a computer, but otherwise when you refactor you're essentially improving your own hand-written code. Unless a computer has the intelligence and the ability to discern code like a human, then no, it's not possible.

Perhaps you're referring to something else?

Upvotes: 1

Related Questions