Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

the "@" indicates plural hence "s" is redundant

by princepawn (Parson)
on Oct 30, 2002 at 16:37 UTC ( #209136=perlmeditation: print w/replies, xml ) Need Help??

I have been meaning to write this post for a long time. In fact, my motivation for this post started about 6 years ago when I was using a program which converted English descriptions to C language expressions. One interesting thing about this program was the way it required you to write about arrays which was different from the way we would describe them in English. For example in English, we think nothing of saying "bag of potatoes". And then moving on to Perl, we might also think nothing of saying "array of integers" which we might think is equivalent to the Perl sentence:
@int = ( 2 .. 5 ) ;
But this particular converter would balk at you. It would say that it is more precise to say you have an array in which each element is an integer, not an "array of integers".

Stated another way, plurality is implicit in the word "array" or the symbol @ It is redundant to pluralize the name of the array.

Now, for the node that re-stimulated this thought pattern: the title was $functions xor $methods. But let's think: a scalar holds a single value, right? Therefore most people would correct this as

@functions or @methods

But me personally, I would write it as

@function or @method
because the plurality is indicated by the @sign.

update Finally, the way that we modify nouns for plurality is not done in chinese. You figure out that something is plural by the number of them mentioned or by something like Perl's @ sign.

Replies are listed 'Best First'.
Re: the "@" indicates plural hence "s" is redundant
by thelenm (Vicar) on Oct 30, 2002 at 16:52 UTC
    I can see your point, but I think my brain works differently. :-)

    I write the names of arrays with a trailing "s", because seeing something like @function really throws me off. It looks like there is some sort of mismatch between the name of the variable and its plurality. To me, the "s" isn't redundant... it confirms at a glance that there are (possibly) multiple elements in the variable.

    Of course, sometimes even a scalar variable can hold a reference to multiple elements (as an array reference, for example). In this case, there is no "@" to indicate plurality, so the "s" is necessary (in my mind, anyway). So, in my coding style,

    • $function contains a single element
    • $functions is probably a reference to an array containing multiple elements
    • @function is a type mismatch in my brain, so I don't use it
    • @functions is an array of multiple elements

    That's not to say that either style is necessarily better. Mine just fits my brain better. I'm curious to see what other people think, as well.

    -- Mike

    --
    just,my${.02}

      I have another reason for not using an "s": I generally write
      $x = $item[3];
      because
      $x = $items[3];
      doesn't make any sense.
        $x = $items[3];

        $x is the third element of this collection of items. No problem. Except my off by one error. ;)

        Really, in my mind, as I'm typing $items, I'm thinkink that this is a collection of items, but now I only want this [3] particular one. So keeping the "s" makes sense. YMMV

Re: the "@" indicates plural hence "s" is redundant
by chromatic (Archbishop) on Oct 30, 2002 at 17:08 UTC
    plurality is implicit in the word "array" or the symbol @

    Untrue. It denotes listishness. (For the purpose of argument, I consider an array to be a very special type of list.) Consider a single element array slice -- not plural, but still a list:

    my @time; @time[0] = localtime;

    That's a rather useless snippet of code, but it demonstrates the point.

    Update: The naming suggestions make sense, and will make even more sense in Perl 6.

      You are right. However, consider
      @numbers= 5;
      Here we also see plurality implied but not realized.
"Redundant" ne "Bad"
by TheDamian (Priest) on Oct 30, 2002 at 20:22 UTC
    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
      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".
      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.
        

Re: the "@" indicates plural hence "s" is redundant
by Molt (Chaplain) on Oct 30, 2002 at 16:53 UTC

    Personally I tend to add the 's' to make things plural, it means I can just 'Read the words' to turn much of the quickly-reread code into English without needing to play about with learning to read the @ as plural.

    I'd say this is also the reason why people tend to use plurals for database tables, despite the fact that it's a table and hence plural.. it's just more natural to write select firstname from users where lastname='Jones' than select firstname from user where lastname='Jones'.

    This can kind of mess up when you index admittedly, $users[0] just seems subtly wrong to my brain, with the plural suggesting I'm dealing with more than one user at this time. Generally I tend to end up with things such as foreach my $user (@users) though, which I find perfectly readable.

    Guess this is one of those personal taste things. Ultimately it doesn't matter providing you're consistant. These are the things of which coding style guidelines are made.

(tye)Re: the "@" indicates plural hence "s" is redundant
by tye (Sage) on Oct 30, 2002 at 17:16 UTC

    I agree. I take it even further and suggest you usually avoid the plural "s" in all program identifiers. It just creates a constant source of doubt ("Did we call that 'user' or 'users'?"). If you just have a "no plural 's'" rule, then you don't waste time trying to remember particular answers to that question repeatedly.

    And, going back to the case of arrays, $users[5] doesn't read as well as $user[5]. So using @users can lead to more code that is less like English if you use elements of the array more than the array itself.

            - tye
      So using @users can lead to more code that is less like English if you use elements of the array more than the array itself.

      I have been guilty of taking this concept of "how you will use it" to the extreme of naming collective variables with an 's' if I primarily expect to act on the list as a whole (or iterating over it), but naming without an 's' when I expect to reference individual elements a lot.

      Fortunately, I'm usually coding alone. :-)


      Impossible Robot
      I have been doing just that for a while: no plurals in variable names, although functions may have it. I also avoid any caps: sub ReallyHardToReadStyleForLongNames vs sub much_easier_to_read_style_even_for_long_names.

      Makeshifts last the longest.

        I'm afraid that, for now at least, I will continue to use

        MuchEasierToTypeLongNames rather than

        a_pain_in_the_arse_to_type_long_names.

        Maybe I have simply been using the former so long that I don't have any difficulty to parsing them, though I think the exaggerated length is a part of the problem.

        isaThing(); as opposed to

        is_a_thing(); or

        $dataFile = './data'; $logFile = './log';
        versus
        $data_file = './data'; $log_file = './log';
        or even

        $AoA = [[],[],[]]; versus

        $a_o_a = [[],[],[]];

        all seem perfectly acceptable choices to my eyes, but more importantly, my fingers. Interupting the flow of typing the name in order to hit shift-- is just to darn awkward I find.

        Maybe once we get full ucs support in Perl, we'll be able to rewire our editors so that the space bar produces the utf equivalent of   (I tried to google it, but its not an easy thing to search for :), then we could have variable names like

        $much easier to read long variable++;

        Though I guess during the transition, this could be fraught with problems.


        Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

        I have to admit that ReallyHardToReadStyleForLongNames or much_easier_to_read_style_even_for_long_names - indeed anything much bigger than slightly_long_name - are on my personal list of Code Smells.

        A variable name that's that long tends to indicate to me that there is some piece of abstraction that I'm missing.

        (oh yes, I prefer "_" too :-)

Re: the "@" indicates plural hence "s" is redundant
by hsmyers (Canon) on Oct 30, 2002 at 19:36 UTC

    It occurs to me that in some ideal sense, it might be nice to have the language figure out the context. For instance, if $user[5] is supposed to mean 'user' number 5, shouldn't that imply that the array name then be @users? I make no claims as to the rationality of this, let alone what it would take to implement same--just a momentary notion (possibly a stroke pre-cursor!)

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: the "@" indicates plural hence "s" is redundant
by Juerd (Abbot) on Oct 31, 2002 at 08:48 UTC

    I like plurality in code, when appropriate. I think it's appropriate in array names. When I say code out loud, I tend to assume the listener is skilled in Perl and knows where to put which interpunction. 'arrays' will be @arrays and 'array' will be $array. The latter implies that it is a reference to an actual array, as scalars can't hold arrays.

    I go a little further than just variable names. Often, I use plural function names to indicate that the function will return a list. In my DBIx::Simple, you can see what I mean: $result->hash will return a single hash (again, that being a reference to one is implied: a method cannot return a real hash), $result->hashes will return a list of hash(referenc)es.

    I don't know any Chinese, and maybe I just want plurality because every language I know changes words (well, not always (sheep, fish)) when they become plural. 'five cow' or 'three car' just doesn't make sense to me, even though I should know there are multiple cows and cars, because of the numbers in front of them. @cow and @car seems strange. I prefer @cows and @cars.

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

      'five cow' or 'three car' just doesn't make sense to me
      No, because that would be "fifth cow" and "third car". :-) You might also read @cow as "many a cow".

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://209136]
Approved by valdez
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-09-28 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?