Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

About the use of the plural form for the name of variables

by grondilu (Pilgrim)
on Nov 08, 2012 at 11:53 UTC ( #1002881=perlquestion: print w/ replies, xml ) Need Help??
grondilu has asked for the wisdom of the Perl Monks concerning the following question:

This is a question about style. Should I use a plural when talking about an array of things?

I mean, the '@' sigil is already here to mean that I'm talking about several things. Using an 's' at the end of the variable is kind of redondant.

So, what writing style would you recommend? This one:
my @friend = qw(john steve carl); for my $friend (@friend) { say "Hello, $friend!"; }
Or this one:
my @friends = qw(john steve carl); for my $friend (@friends) { say "Hello, $friend!"; }

??

Comment on About the use of the plural form for the name of variables
Select or Download Code
Re: About the use of the plural form for the name of variables
by Anonymous Monk on Nov 08, 2012 at 12:00 UTC

    Perl:  for my $friend ( @friend ) ...

    English: for my $friend of array of friends

    English: for my $friend from friend array

    English: for my $friend

    Hmm, Perl Best Practices for naming variables says use %vegetable in the singular

    I think great, but its not a table, use %veg :)

      I think great, but its not a table, use %veg :)

      Excellent point.

      Hmm, Perl Best Practices for naming variables says use %vegetable in the singular

      This made me chuckle. It lead to me imagining Dr. Conway digging his own grave just so he could roll over in it. I know he went to a lot of effort to provide justifications for each "rule" he included. And I know he went to a lot of effort to document that his intent was not to have people blindly parroting rules without considering the justifications and whether or not each applied to their particular environment / situation / style, etc.

      And yet, the vast majority of references to Perl Best Practices that I see are cases of people blindly parroting rules from it. (Perl::Critic is perhaps the most egregious example of this.)

      But I give you points for providing a link. The link did include a justification:

      Damian also recommends naming hashes in the singular, the idea being that individual accesses seem more natural: $vegetable{spinach}.

      Note that I already said that I don't mind having the pluralizing 's' (or other suffix) being immediately followed by a "one of" syntax element (like [1] or {spinach}). They end up right next to each other and so I find no confusion results.

      But I also find $vegetable{spinach} to be an example of a lousy use of a variable name. What are you tracking about vegetables, including spinach? Which of the things that you are tracking do I get back from $vegetable{spinach} ? I guess you might have a bunch of Whatever::Vegetable objects, which would make the name less bad.

      I've seen a recommendation to use prepositions in the names of hash variables. For example, my %vegetable_obj_from_name;. But I realize that I relatively rarely have multiple hashes indexing into the same space of values. So I'm more likely to go with your advice of "It is a variable of limited scope, give it a name that is somewhat mnemonic but also short and thus faster to type and even to read... like %veg".

      Thanks for the reality check and for the chuckle. (:

      - tye        

        The reasoning from your example is obvious; Google only returns "vegetable hash" recipes, but not "vegetables hash" recipes. :-P
        A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: About the use of the plural form for the name of variables
by parv (Priest) on Nov 08, 2012 at 12:16 UTC
    # My preference. @p = ( 'p' , 'pea' ); $ps = [ 'p' , 'pea' ]; %p = ( 'p' => 'pea' ); $ps = { 'p' => 'pea' };
Re: About the use of the plural form for the name of variables
by DrHyde (Prior) on Nov 08, 2012 at 12:18 UTC
    Just like with the perennial arguments about whether database table names should be singular or plural, it's a matter of taste. The correct answer is that it doesn't matter, provided that you're consistent.
Re: About the use of the plural form for the name of variables
by wirito (Acolyte) on Nov 08, 2012 at 13:57 UTC
    I like to use the plurals for arrays when it refers to a collections of things, because an array can be a single thing:
    my @files; my @file1 = qw{ /etc passwd root root 0644 }; my @file2 = qw{ /etc group root root 0644 }; push @files, \@file1, \@file2;
Re: About the use of the plural form for the name of variables
by Pizentios (Scribe) on Nov 08, 2012 at 14:12 UTC
    Personally i don't think it matters as long as the code is easy to read and makes sense (and if your working in a team environment it should be readable by others).

    It doesn't matter in the end of the day as long as the code works and makes sense and is well documented where it needs to be...
    -Pizentios
Re: About the use of the plural form for the name of variables (only vars)
by tye (Cardinal) on Nov 08, 2012 at 14:46 UTC

    My main objection to the use of plurals almost anywhere in software, is that it usually leads to wasted time having to look up whether the plural or singular was used this time (because it is easy to remember the name but very easy to forget whether the name was plural or not). But variables usually have very limited scope, so that means that this problem doesn't apply for most variables.

    And I am at least somewhat reluctant to give two different variables the same name in overlapping scopes, so writing for my $friend ( @friend ) gives me pause. I've certainly done it from time to time. But I've also often enough regretted it. It is a pain to not be able to, for example, rename one of the variables without having your search also stop on all the uses of the other variable (say when you change from an array to a hash).

    I also find it more reasonable to reconcile $fiends[1] over @friend, since the "one of" modifier (like [1]) get placed right there next to the pluralizing letter(s). :)

    - tye        

Re: About the use of the plural form for the name of variables
by choroba (Abbot) on Nov 08, 2012 at 15:22 UTC
    The main problem of the first sample is it uses the same name for the scalar and the array. Avoid such situations, whether you use plurals for arrays or not.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Being able to use the same name for two variables, one array and one scalar, is something I actually like about Perl.

      One thing I don't like about programming is that I always have to think about variable names. So to me any way to reduce the need to do it is good news.

Re: About the use of the plural form for the name of variables
by talexb (Canon) on Nov 08, 2012 at 17:58 UTC

    Good question -- to me it shows that you're taking some time to think about how the code will read, and that's excellent! Future developers will thank you.

    My suggestion: write it so that when you read it back (in your head), it makes the most sense (keeping in mind that this language was written by a linguist). For me, that means you'd use @friends for your list.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002881]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-10-24 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls