I mostly do OO programming, so I'll speak to that. In general there is no magic bullet and the only way to guarantee code quality over time is to refactor. That's just how it is. That being said in practical terms there are some ways you need to be diligent.
Resist the "oh it's just one more...." urge. Whether that means a new method, data, condition, etc. If the scope of a class is increasing past what originally intended then split it up! No need to go crazy. When a class is becoming a PIT due to too much code or lack of flexibility then fix it. Don't plan too far in advance. This is hard and the urge will be strong but don't give in (unless the business demands it.. gotta make that $$$$).
When you encounter a situation where you clearly get a benefit by making things more flexible. Do it. Refactor. Don't wait it will only get worse.
I struggle with this all the time. In my opinion don't think too much about it and just do what feels right. You'll know.