Megadittos.
What he said, etc.
My 2 cents:
I have an AWFUL habit of doing a large loop to start off with,
because I would think the code was "throw-away". But I throw away
NOTHING!
So I typically have a rule that if I have to page down more than once,
ie: about 50 lines, it has crossed the threshhold of "stateful"
information that I can hold in my head. I will then attempt to
isolate and construct functions to be called.
There are some things that I construct as functions on
auto-pilot. Like reading in specialized control information,
etc.
I pretend that I an a performance focused programmer. After all,
the files I deal with are very large and I am very impatient.
Yet I code in Perl, so I KNOW I've made the trade-off between
execution speed and programmer productivity.
So I will code for my 2 screen limit, and then benchmark.
Speed usually comes from such a small part of the code
being optimized that it does not make sense to go through
the effort of optimizing everything.