|Do you know where your variables are?|
Normally I start by making empty subroutines, with comments and print statements on what each piece should do. I don't worry about how it looks or how goofy my naming is, but I do try to put it into the flow and order I am looking for.
At the same time, in another file (like a table of contents or a master record), I put the name of the routine and a list of all the places I call it. I ignore certain super common pieces (like my own rolled cgi parser). And usually because I am working against databases, I work on the schemas for the data.
All along I am preparing a list of questions for the client.
Then I'll go through and make scripts, again empty, that are the framework of the real scripts with ridiculous comments. Usually while I am doing this I am adding and modifying the database schema and the subroutines.
Once I have all this schtuff sort of laid out, I go through and format indentation, making the scripts and bits legal (but not necessarily functional). Again, relooking at things means I am modifying again.
It's usually at this point I actually try to run things. Of course nothing actually works but a bunch of now completely inane prints....
So I print the code. And save off a copy. Send the questions to the client.
And go and do something completely different for at least a day.
I have found if I concentrate on something like this, I cannot comprehend the overall flow, but instead focus on the little details, so a day away gives my addled brain a chance to work it through. The printout is nice cause I can make notes on it, without actually sitting down and trying to work out code bits.
Then I go back through it, make whatever changes make sense and print it again.
At this point I start actually adding working code, parts at a time, testing along the way, right on top of all the faux schtuff I have already written out.
Usually I'll spend 10 to 30 percent of my time in the working out the flow phase and sometimes what I end up actually resembles what I started with.
As far as what I do when my code is ummm....disorganized...I deliver something mostly - partially - somewhat - working schtuff usually before schedule, ask for changes, and rewrite using the above method a second time. The second time through goes a lot faster, and I have lots of code to put into place, usually with mostly minor self induced changes.
Usually the before schedule version buys me enough time to make it nice if I have to go over schedule. If not, I apologize and bite the bullet. Fortunately, I get better all the time so these things happen less frequently.