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 !
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"
My personal rule of thumb is to refactor anything that meets at least one of the following criteria.
- Similar code used at least thrice in same source file.
- Similar code used at least twice in different source files.
- 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.