Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

"Redundant" ne "Bad"

by TheDamian (Priest)
on Oct 30, 2002 at 20:22 UTC ( #209222=note: print w/ replies, xml ) Need Help??


in reply to the "@" indicates plural hence "s" is redundant

In Perl 5, sigils are more like qualifier adjectives that plural modifiers. I always read $ as "that" and @ as "those". So using plural forms for arrays makes perfect sense to me:

$num = 7; # that num is 7 @nums = 1..7; # those nums are 1..7 @squares # those squares = map $_**2, @nums; # are those nums squared $max # that max = $squares[6]; # is that element at index 6 of squares


Comment on "Redundant" ne "Bad"
Download Code
Re: "Redundant" ne "Bad"
by thunders (Priest) on Oct 30, 2002 at 20:43 UTC
    i'll agree with that. especially in for loops.
    for my $widget(@widgets){ do_somthing($widget); }
    reads to me as "for each widget of these widgets, do something with this widget" not perfect english, but the plurals helps me keep the element from being confused with the array. though i guess in some cases i'd use the same name.:-)
    for my $deer(@deer){}

      This makes perfect sense to me - using plurality in context.

      Looking back at the original example, while:

      @int = ( 2 .. 5 ) ;

      might be thought of as "an array of integers", it falls back on Hungarian style notation (int) which denotes a "data type" (singular), not "data types" (plural). Following that line of thinking, it could also be thought of as "an array of values of the integer data type ". Of course this doesn't read as easily as "an array of integers", and just goes to show that programming conventions don't always neatly fit into the syntax of the English language.

      C'est la vie!

      Acolyte
      Studying at the feet of the masters
      I would rewrite
      for my $widget(@widgets){ do_somthing($widget); }
      as
      foreach (@widget) { do_something($_); }
      because the latter makes sense in English as well as Perl if you realise that Perl uses $_ to say 'it': "For each widget, do something to it".
Re: "Redundant" ne "Bad"
by earthboundmisfit (Chaplain) on Oct 31, 2002 at 00:48 UTC
    At first, I was obdurately convinced that this is the only "correct" way to think of array naming. The author's rep may have influenced my conviction slightly ;) But on the drive home tonight it suddenly dawned on me that there really is something to what princepawn is saying, something I couldn't see or understand at first. Consider:
    a murder of crows
    a surfeit of skunks
    a bag of potatoes
    
    You wouldn't pluralize 'murder' or 'surfeit' if you were referring to just one collective of crows or skunks. Stated another way and to further explain what I think princepawn means is 'it's the potatoes that gets the plural 's' not the thing holding them.' It's sort of a reverse synecdoche that leads us to pluralize the array name.

    Funny how I feel no compulsion to pluralize the array name when I actually use the word 'array' in it:

    @myArray = qw(crow skunk soldier); @codeArray = (&get_code(3,500)); my @bigAssArray = qw(donkey mule burro);
    That said, I personally cannot shake the readability of using pluralized array names and will most likely continue to do so unless, of course, I'm using some crazy English to C translator that can't distinguish what I mean from what I say ;)

      Funny how I feel no compulsion to pluralize the array name when I actually use the word 'array' in it.

      I never use the word 'array' in an array name. It's completely redundant. The same goes for hashes and scalars: I never have %function_hash or $username_scalar.

      I do use 'array' and 'hash' to name references. If you read $array in my code, you can be sure it's a reference to an array. Some people use $arrayref or even $array_reference, but 'ref' is useless information, imho, because a scalar simply cannot be an array. (Okay, I'm ignoring overloading now)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-09-30 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls