in reply to Re: (2) Thoughts on naming loop LABEL: (Why I name hash vars singular. Why label loops?)
in thread Thoughts on naming loop LABEL:

Actually I disagree with AM below (So why am I replying to you? TIMTOPTPI ;-) I think judicious use of labels helps code legibility a great deal, even when the loops aren't nested.

For example, when I'm processing a result set from a database, I often label the loop RECORD: or ROW:. That way, after some long block of code, I can write next ROW; to make it clear I'm short-circuiting the rest of the logic. It's the same reason I choose variable names carefully: it helps me (five years from now when I'm re-reading it) keep track of what's going on.

Replies are listed 'Best First'.
Re: Re (3): Thoughts on naming loop LABEL: (TMTOWTDI, why label loops?)
by Anonymous Monk on May 02, 2002 at 17:06 UTC
    Do you label all such blocks or loops to make it clear that you are short circuiting the loop when you have a next, last, or redo?

    LINE: while(<>) { # ... next LINE if ... # ... } SEARCH: for (@list) { # ... last SEARCH if ... }

    Do you do so only in long blocks? Complicated blocks? Do you have a consistent style for when and how to label loops or is it more of a "whatever seems good at the time" approach? I ask not to be difficult but because in a single non-nested loop I just don't see any additional clarity being added by loop labels. If you see a benefit can you describe the situations where you find it beneficial versus those where it is ok to not label the loop?

      Hmm, good question. I don't think I have any specific rules, but I seem to do it based on some notion of "distance": if the next or last is "a long way" from the top or bottom of the loop, I'll put in a label. Ultimately, it's a question of "if it looks better with the label, add it", but I can't quantify what I mean by "better".

      On the other hand, I don't think I've ever used a label with redo (I don't use it very often 'cause I find it confusing. ;-)