R – How and How often do you refactor your code

coding-styleprogramming-languagesrefactoring

My question vaguely relates to this one. However, it doesn't address the techniques or practices.

I am reading The Pragmatic Programmer and it strongly advocates refactoring code as often as you can. However, it doesn't go into detail about how to do that and how often. How do you guys go about refactoring your code, and how often do you refactor?

Best Answer

When and where needed.

There is no point on refactoring per se, it is a tool to ease further development. Only refactor when you will get advantage out of it. A small library that will never evolve and is tested is the perfect candidate not to refactor, any time invested on the refactoring will not pay back. Parts of your system/program that will evolve, need maintenance and are hard to read are the clear candidates to refactor.

Always think on the ROI (return of investment), if you start refactoring everything that you now think could be better then tomorrow you will refactor again, and maybe some time you will get time to actually perform some real advance.