http://www.perlmonks.org?node_id=136062


in reply to do/redo or for(;;): what's Kosher?

I find that last, next, and redo can lead to code that is very hard to read/maintain, etc. They are very useful and I use them myself but I try to avoid them and I pretty much refuse to use them in large, complex, and/or nested loops. If I can't see the whole loop on one screen and easily pick out the last, next, and redo, then I don't like to use them.

If I find a need for these in such cases, it usually means that I need to break the code up into smaller subroutines and these usually disappear once I do that. Sometimes they are replaced by an early return from a subroutine which is something I don't feel any need to try to avoid.

I really dislike using a naked block with redo as a "loop". It feels mostly like using goto to make a loop. They are both very powerful; there isn't any control flow that can't be constructed with either. That is a nice feature, but it is also a liability. I've seen several cases of good programmers that jumped directly to naked-block+redo and produced code that worked but was quite complex and confusing that I was able to turn into a very simple while loop. Because naked-block+redo allows you to create any control structure, no matter how complicated, you can implement your first stab and not notice that you've overlooked a much simpler, cleaner, clearer solution.

Even if you start with a fairly simple and small naked-block+redo "loop", on-going maintenance can shove things around. last and redo can end up being like a goto but worse because you don't have a lable so you can't easily tell where control is being passed to. You have to keep scanning up looking for blocks and seeing which blocks are loops (vs. if blocks, do blocks, etc.) and then perhaps find the matching end of the block.

I also agree with others here that don't think "loop" when they see a naked block and so I don't think I'll ever use a naked block that way in code that will be maintained.

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: do/redo or for(;;): what's Kosher?