Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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

closed as primarily opinion-based by durron597, MichaelT, Snowman, Kilian Foth, GlenH7 Jul 7 at 14:19

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

possible duplicate of When to refactor –  Snowman Jul 7 at 2:33

1 Answer 1

up vote 18 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
"If you treat refactoring as its own thing, rather than as part of the process of development, then it can become never ending" ... Otoh, "It seems that perfection is attained, not when there is nothing more to add, but when there is nothing more to take away" - Antoine de Saint Exupéry ;-) –  Mawg Apr 28 at 12:25

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