Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Thoughts on naming loop LABEL:

by ybiC (Prior)
on May 02, 2002 at 12:31 UTC ( [id://163517]=perlquestion: print w/replies, xml ) Need Help??

ybiC has asked for the wisdom of the Perl Monks concerning the following question:

Over the past almost 2 years here, I've picked up the following general guidelines for naming:
$singular_noun # scalar @plural_noun # array $singular_noun{specific} # hash, element sub verb{ ... } # subroutine
Having recently learned of loop LABELS:*, I'd welcome your thoughts on logical and/or reasonable naming approaches for such.   Here's a snippet to give some of context:
my $pb = HTML::TokeParser->new(\$content); my $regex = '^\d+$'; LABEL: while($pb->get_tag('b', '/b')){ my $tb = $pb->get_text(); $foo = $tb; last LABEL if $tb =~ /$regex/; }
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

* erikharrison++ for Re: Exit while loop on first match

Replies are listed 'Best First'.
Re: Thoughts on naming loop LABEL:
by rinceWind (Monsignor) on May 02, 2002 at 12:38 UTC
    ybiC,

    My thoughts run along the line of function. What is the label doing.

    Usually, the label is referring to iteration round a loop. What is the iteration? I guess it is also a noun, but in CAPS to distinguish from the scalar loop variable (but they could still be the same English word).

    next ITEM; last ATTEMPT; redo GAME_TURN;
    my $0.02 --rW
Re: Thoughts on naming loop LABEL:
by ariels (Curate) on May 02, 2002 at 12:54 UTC

    I (try to) opt for a "rôle-based" naming style; some might call it "punning". Since the vast majority of my loops aren't labeled, there's always a reason for labelling those that are. That reason is the operation I wish to perform on it: redo, next, or last (you'll see goto doesn't really fit into this mold, but I use it even less).

    Name the loop to make the "something LABEL" phrase self-explanatory. For instance, in your example you could name the loop TAG; "last TAG" means just that. So would "next TAG" and "redo TAG".

    rinceWind suggests much the same thing, for seemingly different reasons. Perhaps some linguist can help us make sense of whether they are, in fact, different reasons?


    I find your variable naming style somewhat inconsistent (=~ /^I do it differently$/). You name arrays in plural, but hashes in singular. Your example is biased: you give "@plural_noun", referring to the entire collection, and "$singular_noun{specific}", referring to an element. I try to keep everything singular; my arrays tend to be "@singular_noun", so that "$singular_noun[INDEX] makes singular sense. Plurality or singularity stem from my choice of line noise: $, @ or %. And I don't pluralise twice.

Re: Thoughts on naming loop LABEL:
by talexb (Chancellor) on May 02, 2002 at 13:19 UTC
    I want to respond to the naming convention .. my preference is to use plurals if it's not a scalar, so
    my $FileName; my @Lines = <HTML>; my %MenuSelections;
    As far as labels go, answer the question "What's happening here" (but without getting too humourous -- a client might be reading this code some day). In your example I would use READ_TAGS because you are looping and reading tags.

    Naming subroutines is the same deal: "What does it do?" Something called sub ReadData may make sense at the time, and maybe sub ReadFinancialData is lots more to type, but look how cool it looks when you review the code a few months later. All is explained. Lovely.

    For me, it all comes back to making the signal to noise ratio in the source code as high as possible. My variable declarations at the beginning of this post read as "My filename, my lines, my menu selections", and that makes sense to me.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: Thoughts on naming loop LABEL:
by dreadpiratepeter (Priest) on May 02, 2002 at 13:35 UTC
    I'm curious as to why you use a singular noun for a hash, which is a collection of things? I use a plural.
       $users{fred} and %users
    sound better to me than:
       $user{fred} and %user

    The exception to this is when I'm using a hash to represent attributes of an object. ie:
       $user{ADDRESS}, $user{PHONE}

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
      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"; }

        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

        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.

Re: Thoughts on naming loop LABEL:
by Anonymous Monk on May 02, 2002 at 13:39 UTC
    I've been using Perl heavily for many years and have never once had the need to use a LABEL in production code. Granted, it has always been nice to know I could if I needed it. But I would never use a LABEL on a non-nested loop block, it serves no semantic purpose and does not help "self-document" a block of code. If you think otherwise then you have to ask yourself why you don't label or name every block of your code?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found