I needed some code in a third place, so I decided to extract the ~100 lines it into a single function in common code (and make it more efficient, because I had seen it in profiles under stress testing).
I often write code by starting it and seeing where it leads: this is helped by modern revision control and fast compile times (I point this out because we take these for granted).
First extraction went fine. Then I tried to use the same code in the second place. By the time I'd had to add four callback hooks (three of which were unneeded in the first case) I stopped. The code was different enough that the "framework with callbacks" made the code far *less* clear.
Threw it away, in favor of a handful of helper routines. More code for the first case, but much more natural for the second.
This took a day, on and off. But the off was important: the breaks gave me time to ask "now it's almost finished, do I like it?" and avoid being too goal driven.
.
