Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by ybiC (Prior)
on May 02, 2002 at 14:41 UTC ( [id://163549]=note: print w/replies, xml ) Need Help??


in reply to Re: Thoughts on naming loop LABEL:
in thread Thoughts on naming loop LABEL:

Good question, dreadpiratepeter - I suppose it's largely a matter of taste.   I only meant to preface my question with examples of my currently preferred naming style, not say that mine is necessarily better than any others.   I'm sure there are other threads on the matter, but only have time for a superficial Search, which turned up Sinister's recent Plural variable naming (or not?).

I forgot to ask the probably greater question that AM points out below - *why* would one choose to LABEL: a loop?

    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

Update: Now that I've a bit of time, I'll try to es'plain my naming preference of singular-for-a-hash-var:

It seems to boil down to what looks more clear in the context of how I most commonly employ the variable types in my code.   In the following bogo-code, each $hash{key} is dealt with as an individual (singular) item.

my %param = ( srcdir => '/home/me/perl/', destdir => '/root/perl/', moduledir => 'bar-foo', tarball => 'bar-foo.tgz', ); cd $param{srcdir}; system /usr/bin/tar $param{tarball} &param{moduledir} -zcvf and die $! +; system /bin/mv $param{tarball} $param{destdir} and die $!; cd $param{destdir}; system /usr/bin/tar $param{tarball} -zxvf and dir $!

Then in this next kind of hash usage, I still think of each $hash{key} as an individual item.   And in the loop, the "keys" conveys the plurality of $hash{key}'s within the greater %hash.   At least to me.   Heh.

my %info = ( ' script' => "$0", ' executable' => "$^X $]", ' hostOS' => "$^O", ' starttime' => "$^T", ); for (keys %info) { print $_ $info{$key}, "\n"; }

  • Comment on Re: (2) Thoughts on naming loop LABEL: (Why I name hash vars singular. Why label loops?)
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: (2) Thoughts on naming loop LABEL: (TMTOWTDI, why label loops?)
by cfreak (Chaplain) on May 02, 2002 at 15:02 UTC

    Labeling can be very useful. Consider this:

    @array = (# some values that could contain an error) MAIN:while(1) { # something to keep this non-existant # program running foreach(@array) { last MAIN if($_ eq 'some error'); } }

    I hope thats a good example. (I know there are better ways to do what I just did but just wanted to show how LABELs can be used). They can also be useful for just seeing what loop you are in if you have several nested

    Some clever or funny quote here.
    Chris

Re (3): Thoughts on naming loop LABEL: (TMTOWTDI, why label loops?)
by VSarkiss (Monsignor) on May 02, 2002 at 15:04 UTC

    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.

      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. ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://163549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-24 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found