views:

69

answers:

2

What ever i am coding is it a Refactored code or not ? This question always makes me think twice while coding in Rails . When Should one apply Code Refactoring techniques in rails ? ,Are there any best practices for refactoring ? , Scenarios which you have come across your development experience will help people coding Professionally and this could create a road map for coders state change from beginners to Professionals . Thanks !

+2  A: 

Refactoring technics is not language specific, they are general for most of the languages. You could read about them in the famous book written by Martin Fowler: "Refactoring: Improving the Design of Existing Code"

Restuta
@Restuta Thanks for your answer i do agree with you ,I am reading it too :). I would like to Know instead of Refactoring the existing code , Can we have an idea of Design in mind and code accordingly.
YetAnotherCoder
Gotcha, think this is not necessary cos development process should be write test - write code - ensure that test were satisfied - refactor code - run tests.
Restuta
+1  A: 

My personal rule of thumb is to refactor anything that meets at least one of the following criteria.

  1. Similar code used at least thrice in same source file.
  2. Similar code used at least twice in different source files.
  3. Long method calls, where one statement spans more than 2 lines and the method is consistently called with most of the same options/arguments.

As for how this fits into my workflow. I'll usually switch gears to refactor as soon as I notice one of those three criteria being met. The reasoning behind this change is that should I notice something is already common enough to refactor before I finish with the task at hand, there is a very good chance I'm going to use that similar code again. So I'm probably going to save some time, effort and headaches in the long run.

With a dynamic language like Ruby it makes sense to refeactor a little more often. As in if you have several similar accessors that require a little more computation than just attr_accessor, you can write an eval block to define all of them at once. I find that any change I make to one of these methods have to be made to all. Why not do it all at once with a bit of metaprogramming.

EmFi