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/
> 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/