This may seem like a bit of a cool argument, but I think this is true in my experience:
Refactor it a lot.
Many of the answers here (especially a strong test suite) are great advice and help a lot, so let me make it clear that I am also for these preventative measures.
Firstly, it is always easy to change (when it is small). Then, as a rule, you need to go through several bouts of complex refactoring before you have a breakthrough and get something really flexible.
source share