When you think of refactoring, you imagine it in the context of TDD. Before working on a feature, you first write a failing test. Then you make it pass by writing code that implements that feature. Finally, you refactor the working code to make it more readable, clean, and beautiful.
In our industry, it’s known as red-green-refactor. You start with red (failing test), and move to green (working code) code, ending with refactored code with a better design. However, there are some other times when refactoring code can be very useful. This post explores a few.