When should we refactor?
- Because you planned to, after a test passed*.
- Something smells bad.
- You need to add something, and want to make it easier.
- Your pair wants to understand the code better.
- You want to try an experiment with a different structure.
- The code doesn't say something that it should
- The code does say something that it shouldn't
- The code says something more than once.
- You want to make it easier to test.
- Fixing bugs needs a lot of time. <-- please clarify
- ...
* if you refactored before it passed - meaning if you integrated new behavior into existing behavior - you would be integrating untested behavior. Only integrate tested behavior.
See also SourcesOfRework (which refactor is not)
CategoryRefactoring