Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I don't know if it's just with me, but when I start refactoring some code I waste a lot time doing that and it never ends.

Every time I read the code again, I find something that can be improved, code refactoring is like my brain into a infinite loop and the end conditional is reached randomly with no satisfaction.

So, how much time should I dedicate to refactoring my code?

share|improve this question
add comment

1 Answer 1

up vote 12 down vote accepted

If you treat refactoring as its own thing, rather than as part of the process of development, then it can become never ending.

If you follow the test driven development approach of red, green, refactor, then you typically won't have to spend a lot of time refactoring for two reasons:

  1. You'll be refactoring as you go, so it shouldn't become a mammoth task, just another small step in the TDD cycle.

  2. The internal "pressure" to get the next test written will keep your refactoring efforts to a sensible level.

If you are working with legacy code, then follow Uncle Bob's "Boy scout rule." Timebox yourself to a short time period (say, 30 minutes) and make your goal to check in the code in a cleaner state than it was before. Each time you have to touch the code, you will leave it slightly better. Over time, the most modified code will be the most readable and well factored in your codebase.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.