http://www.perlmonks.org?node_id=152710

Monks,

I am just writing down some thought here below. If you want to take it serious, be my guest... ;-)

I have been wondering on the naming of variables. Not so much what to call them (Non ambigious, descriptive, not to long and to to short) but wheter to use plurals for different types of variables.

An array holds a list of variable values of the same context, say : database fields. The context of the program could lead us to suspect that all is database related, and thus you choose the name fields. This is logic. Now we come to hashes. A hash is nothing more then a noble array, but the context of the hash could be much wider. If you want a hash with database fields, and a context specifier in the value of each key, would we then call the hash 'field' or 'fields'. I agree that the entire hash contains fields, and it should thus be plural, but when we put the hash to use, field is more appropriate. (eg: $fields{ID} or $field{ID}).

Hmm... Now the ideal plural for the array is no longer that logic, cause when I start to use the array litteraly in my code ($fields[0]) and there is but one thing in that item, the naming has suddenly become ambigious.

It seems that one has to think deeper on his variable naming for list storing variables (hash or array) then seems apparent. If you store field names in an array, and you wish to use the values distinct in your code, you must call the array @field, cause it makes more sence. If you wish to apply the entire list (eg: foreach my $field (@fields) { ... }) You must name you array @fields.
With the use of hashes I think avoiding plurals in naming them is best, since you won't just use the keys of a hash for lists in foreach, you will actualy dive deeper, and plurals will (mostly) become ambigious and non-descriptive...

*sigh*

UPDATE : Saw the flaw, ChOas pointed out, and took over simon.proctor's syntax...

er formait hyarya.
"Field experience is something you don't get until just after you need it."

Replies are listed 'Best First'.
•Re: Plural variable naming (or not?)
by merlyn (Sage) on Mar 19, 2002 at 14:46 UTC
    I support $singular and @plurals as a general convention. However, for hashes, I tend to think of hashes as providing a mapping function between a key and its value, so I frequently name it as a f() style, as in %last_name_for or %last_name_of or %has_subentries. That makes it clear to the reader that I'm holding a mapping in that hash.

    -- Randal L. Schwartz, Perl hacker

      For the $singular and @plurals, I had independently come to Randal's style. I haven't used his style for hashes, but it looks good and I am going to try it.
        Only a mere 14 years later!

        -- Randal L. Schwartz, Perl hacker

        The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: Plural variable naming (or not?)
by tachyon (Chancellor) on Mar 19, 2002 at 14:57 UTC

    The rules for variable naming are based on (in no particular order ;-)

    • Company policy
    • Project policy
    • Immediate bosses policy
    • Personal preference
    • Language Norms
    • Logic $scalar @arrays %hash(s) FILEHANDLE &increment_widget_value

    One word:

    ->Consistency<-

    There_is_nothing
    worseThan
    code_that
    DoesNotUse
    CONSISTENT_nAmInG_CoNvEnTiOnS.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Plural variable naming (or not?)
by Juerd (Abbot) on Mar 19, 2002 at 12:27 UTC

    As usual, it's a matter of style :)
    My style is:

    $singular @plural %singular
    With one exception: I often have %subs, a hash that contains sub references. For some reason, I have always put that in plural.

    A good document on style is slashstyle (Note: I don't agree on many points, but it's nice to read, and gives an indication of the many possibilities.)

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      It is not merely a matter of style. It is also a matter of program context, and wheter or not the name is more ambigious with the plural (or singular (thanks for freshing up the mind)) and may not provide as much context as could be intended.

      Therefor a programmer expecting to have his code reviewed or updated should be well considering the use of plural or singular form on each variable name, and the matter of style should be thrown overboard on this specifick isue... (Or should it not...?!?)

      er formait hyarya.
      "Field experience is something you don't get until just after you need it."
Re: Plural variable naming (or not?)
by ChOas (Curate) on Mar 19, 2002 at 11:15 UTC
    Hi!,

    I have have often thought about the same thing, and now I only use non plurals (sorry, lack of English)

    One question:
    If you wish to apply the entire list (eg: foreach (@fields) { ... })
    You must name you array @fields.


    I`m not sure why you say that...

    I think there is nothing wrong with saying "for each field do yadayadayada"
    e.g  foreach(@field)

    Nice to know I`m not the only one thinking about all that stuff :)

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      foreach my $field (@fields)


      Assuming of course you don't want to just stick with $_ :)
Re: Plural variable naming (or not?)
by andreychek (Parson) on Mar 19, 2002 at 18:15 UTC
    I have really come to enjoy the ideas and syntax offered by the P5EE Style Guide.

    In the guide, they offer this for plural vs singular variable naming conventions:
    --- Arrays and hashes should be plural nouns, whether as regular arrays and hashes or array and hash references. Do not name references with ``ref'' or the data type in the name. @stories = (1, 2, 3); # right $comment_ref = [4, 5, 6]; # wrong $comments = [4, 5, 6]; # right $comment = $comments->[0]; # right ---
    By consistantly using a system like this throughout your programs, there won't have to be question about whether a scalar contains something like a string, or if it's a reference to an array or hash. As the guide suggests, references would be named in a plural fashion.

    FWIW, I'm now using (or at least attempting to) this guide for all of my programming habits :-) Have fun,
    -Eric
      By consistantly using a system like this throughout your programs, there won't have to be question about whether a scalar contains something like a string, or if it's a reference to an array or hash.

      7 years later, I have this to say ….

      It seems to me that this offers exactly the benefit you describe, but that that benefit might not be what it seems at first glance. That is, I can tell by looking at a variable name * whether it is

      • a string,
      • or
      • a reference to an array or hash;
      but I cannot answer the question of whether it is
      • a string,
      • a reference to a string,
      • a reference to an array,
      • or
      • a reference to a hash.
      (For that matter, there are all sorts of other references (and, for that matter, other things to stuff in scalars, like numbers), too, like \\1.) It seems to me that this is a level of granularity that one would like, in many cases, to be able to maintain. I don't know a better solution than the pseudo-Hungarian $ima_href, though, and I don't think much of that.

      * Although it's important to note that I can't even necessarily do that—is $moose singular or plural?

Re: Plural variable naming (or not?)
by FoxtrotUniform (Prior) on Mar 19, 2002 at 16:35 UTC

    Like most people here, I usually name $singular, @plural. My hashes are singular if they represent a data structure, and plural if they represent a mapping.

    One exception to the first rule, though, is that I'm perfectly happy to name a scalar in the plural... if it's an arrayref or an appropriate hashref.

    --
    :wq

Re: Plural variable naming (or not?)
by cjf (Parson) on Mar 19, 2002 at 14:53 UTC

    I follow naming conventions roughly similar Juerd and simon.proctor.

    Scalars should be singular, it really throws me off when they're not, $fields just really doesn't look right. Arrays should always be plural. Hashes on the other hand, I'm fairly indifferent about, but I tend to lean towards singular $field{name}. I think this looks a bit better than $fields{name} but it does depend somewhat on what it's being used for.

      Does this look right? Accessing a plural array for a singular element

      my @replies = ( 'so', 'plurals', { 'you' => 'think?' } ); my $answer = $replies[2]->{you}; print $answer;

        Actually, yes, it does :)

        It might only appear right to me because I've become accustomed to doing it that way. I do see your point, but I still find plural array names feel more natural than singular ones:

        my @reply = qw/a singular array name? how odd/; my $cannotnamethisreply = $reply[5]; print $cannotnamethisreply;

        Now that looks stranger to me than using a plural for the array name and a singular for the related scalar. I can understand either view though. :)

      Scalars should be singular, it really throws me off when they're not, $fields just really doesn't look right.

      I agree with you on that one, although sometimes I will use a plural scalar if it's a named reference to an array, e.g.

      $fields = [1, 2, 3];
      That also helps me to remember that it's not a "normal" scalar, but that it's really referring to something plural.
Re: Plural variable naming (or not?)
by beppu (Hermit) on Mar 20, 2002 at 02:55 UTC
    Am I the only one who uses the singular form for arrays?
      No, you aren't: Some names (i often name variables in german, or mix the languages, this i should change!):
      @mathsylist : a name for a list in that i store the recent mathsymbols.
      %preprintliste : the matching preprints, stored as a hash, in order to avoid trouble with doubleentries in two files.
      @sortierteschluessel: this is plural (sorted keys) for exactly that.
      It seems that i either use plural or something like list in the end.

        Interesting... I'd have thought that @plural would be more intuitive than @singular_list. Obviously, your mileage varies.

        The one place I can see where this would be a distinct advantage is when you're using Perl arrays as different data types. For instance:

        my @foo_list; my @bar_stack; my @baz_queue; my @xyzzy_heap;

        If these were just plurals, it would be impossible to tell just from the names which one was a stack, or a queue, or whatever -- which could be a very bad thing.

        --
        :wq

Re: Plural variable naming (or not?)
by JavaFan (Canon) on Dec 11, 2009 at 10:08 UTC
    Just use both:
    use Data::Alias; my @plural = qw[foo bar baz]; alias my @singular = @plural; say $singular[1]; $singular[2] = "qux"; say "@plural"; __END__ bar foo bar qux
      Hi,
      Let's keep this oldies but interesting post running. :)
      PERL has a point in using singular even for arrays. That is the sigil. Saying @persons is superfluous, @ marks it's a list (an array) of something. $car[3] could be read for example as 'Third car in the array' or 'Car number three in the array'.
      Square brackets will reveal if it's an array if in doubt. Consistency is important. Perlstyle could give some advice also in this matter.
      Beers, T