I don't know how most other people code, but I rarely write out a module, even from scratch, in a way that doesn't require editing and rearranging. I start with a skeleton which reflects my best understanding of the problem and then try my best to fill it in with code. Sometimes the organization is not quite right, or sometimes you realize you need an extra parameter somewhere that is out of scope; but whatever it is, there's always some kind of stumbling block that requires reorganization.
The overall shape is usually close enough, but it always requires refinement. That refinement happens in a loop; you move stuff around, do a little nip tuck, maybe attempt to fix some bugs, and then run the module (or the tests). Optimizing this loop is important (it's one of the major benefits of scripting languages); once you figure out what change you should make, or what experiment you should run, the faster you make it happen, the faster you get the results, and the faster you can get back to the important mental aspect of figuring out how to fix or create whatever you're working on.
The overall shape is usually close enough, but it always requires refinement. That refinement happens in a loop; you move stuff around, do a little nip tuck, maybe attempt to fix some bugs, and then run the module (or the tests). Optimizing this loop is important (it's one of the major benefits of scripting languages); once you figure out what change you should make, or what experiment you should run, the faster you make it happen, the faster you get the results, and the faster you can get back to the important mental aspect of figuring out how to fix or create whatever you're working on.