That's a very good example. When you write & document software, you don't normally use state machines. But when you do, it's because that's the best representation of that piece of the problem. And that state machine is the concept that you need to map to a subroutine, which naturally maps to a larger-than-normal subroutine. (Which thankfully has a simple, repetitive form, as you mention.)