Over time, even with the best of intentions, code bases will tend toward entropy if constant effort isn’t made to improve and maintain them. Even revisiting your own code which has sat untouched for years, after your learning and knowledge has expanded, will look worse than you remember leaving it!
Recently I had a suit made to measure, and while the tailor is able to get it fitting pretty near right on the first attempt, he will always need you to try the suit on so he can work out what adjustments to make. The suit you try on is stitched properly, the alterations are made by carefully unpicking the seams being careful not to damage the material. The fabric is repinned and the new seam stitched in.
The same goes for an approach to refactoring code, the seams are the structure of code, the fabric being the logic. In order to change the structure, we must use techniques which leave the logic intact. We build the logic while not worrying too much about the structure of the code, and then refactor to form the exact shape. Just like the tailor we can iterate over this process several times until the final product has been crafted.
The tailor doesn’t worry about the first few iterations being time wasted as it helps him fit the suit to an individual body. Equally refactoring as we learn more about a problem space and find how we structure the code over time, helps us to create a codebase which is malleable and can evolve over time. As the codebase grows, alterations can be made to support the changing nature of the system, and the constant unpicking and restitching prevents devolution towards the mess seen in so many code bases where this has been neglected.