Mailing List Archive

Re: Variable naming
I believe the best naming scheme is to name a variable over what the variable's
content as a whole represents.

If the variable is best described as being a single thing, name it after that
and singular; if it is best described as being a collection whose elements are
each a particular thing, then name the variable plural of its element type.
This same rule should be followed no matter the variable type.

Same goes for SQL databases; a table should be named after what the entire
content of the table is, not after what a single element of it is named, same as
proper array naming.

So proper naming for a Perl hash is whatever the entirety of the hash as a unit
represents, no different than for an array or an object.

-- Darren Duncan

On 2024-01-02 2:43 p.m., Sam Kington wrote:
> On 2 Jan 2024, at 15:53, Philippe Bruhat (BooK) <book@cpan.org> wrote:
>> I'm reminded of https://www.cpan.org/misc/perl.purity.test and the
>> questions in the "Variables" section.
>>
>> Myself, I always giggle a little when I find myself using multiple
>> variables with the same name... And because of the Perl purity test, I
>> sometimes try to find a use for the hash (in addition to the scalar and
>> array), before usually switching to a plural name for the @ and a
>> singular one for the $ (to the relief of my coworkers).
>>
>> (By the way, what is the custom for hashes? Plural or singular?)
>
> The rule of thumb that I use (which I’ll cheerfully admit not everyone in dev
> teams I’ve been in subscribes to) is that arrays should be plural, because
> they’re almost certainly 0 or two+ things; but hashes should be singular, unless
> the *value* is inherently plural, because of *course* you have multiple keys.
>
> So most of the time:
>
> my @all_cities = qw(London Cardiff Edinburgh Belfast);
> my %country_capital = (
>     England            => 'London',
>     Wales              => 'Cardiff',
>     Scotland           => 'Edinburgh',
> 'Northern Ireland' => 'Belfast',
> );
> my $capital_city = $country_capital{get_country()};
>
> but
>
> my %regional_capitals = (
>     gb => [qw(London Cardiff Edinburgh Belfast)],
>     fr => [qw(Ajaccio Bordeaux Dijon Lille), ...],
> );
>
> Sam
> --
> Website: http://www.illuminated.co.uk/
>