At some point a program is in development. Features are being added or removed or changed all the time. Every version is nothing but a prototype. So I don't waste much time on writing super clean code at that point because I never know how long something lasts. Of course I try to keep the code quality to certain standards, but time is always an issue.
Then comes the point where the program is finished and the decision maker(s) say "that's it". I do have a working prototype at this point, but the code inside is a bit messy from all the back and forth during the development phase. I am expected to start testing/final debugging but my gut says I should now somehow clean up and or rewrite stuff to give it proper architecture that makes maintenance etc easier.
Once stuff has been tested and approved, it makes no sense to rewrite then. On a regular basis I am standing there with a working 'finished' prototype and I get a bug during testing and I see that it is a result of not-smart coding which is a result of the whole development process. I am in the middle of testing and the bugfix would be a rewrite... it's a mess!
There are better/textbook ways, I am sure. But i have to work in a real work environment where not everything is textbook.
So how do I transition my working prototype to a release version with a stable code base ? Maybe I should not consider the development finished once I do and actually see it as the clean-up phase... I don't know, I need help here.
EDIT
I want to clarify a few things.
I am 100% on the side of doing it right before and not after, code clean and readable. But i also have to get things done and can't dream about the beauty of code all clean and shiny. I have to find a compromise.
often a new feature is really just something that we want to try out and see if it makes sense to implement something like this. (esp. in mobile apps, to get a real look-and-feel on an actual device) So it is something small that (imho) does not justify too much work in a first "let's see" iteration. However sometimes the question arises WHEN do i pay this tech.debt ? That's what this question is all about.
If I know that half of the features will be dropped one day later (enough experience in our company by now) I really find it hard to believe that the best way to approach my problem is to nonetheless invest extra time to write everything clean even if most of it will be dropped shortly after. It feels to me that I will save time if I do one big cleanup once the thing is solid, hence my question.