Mailing List Archive

Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?)
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/
Re: Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?) [ In reply to ]
On Tue, 2 Jan 2024 22:43:20 +0000
Sam Kington <sam@illuminated.co.uk> wrote:

> From: Sam Kington <sam@illuminated.co.uk>
> To: Perl5 Porters <perl5-porters@perl.org>
> Subject: Variable naming (was: Re: Maybe it's time to disallow two variables
> with the same name and different sigil?) Date: Tue, 2 Jan 2024 22:43:20 +0000
> X-Mailer: Apple Mail (2.3774.300.61.1.2)
>
> 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

hi, Sam!

FYI http://www.illuminated.co.uk/ gives me an nginx 403 error.

--

Shlomi Fish https://www.shlomifish.org/
https://github.com/shlomif/shlomif-computer-settings/ - my dotfiles

In the beginning the Universe was created. This has made a lot of people very
angry and been widely regarded as a bad move.
https://en.wikiquote.org/wiki/The_Hitchhiker's_Guide_to_the_Galaxy

Please reply to list if it's a mailing list post - https://shlom.in/reply .
Re: Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?) [ In reply to ]
On Tue, 2 Jan 2024 at 23:43, Sam Kington <sam@illuminated.co.uk> 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), ...],
> );
>

Yeah, I mostly follow this convention as well. Especially when the map is
serving as a super light mapping function. The counter argument is hashes
where I am likely to use values/keys on the hash, in which case I might
treat it similarly to an array.

Yves

--
perl -Mre=debug -e "/just|another|perl|hacker/"
Re: Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?) [ In reply to ]
On Tue, 2 Jan 2024 at 23:43, Sam Kington <sam@illuminated.co.uk> 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.
>
>
As mentioned in other thread, same arguments as for some (afaik most of)
SQL coding standards uses singular for table name.
You don't care what is name of collection, you do care what single element
of collection represents.

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), ...],
> );
>
>
I tried to solve this in Jelly framework (those who attended APW2014 in
Salzburg may recall my first-ever public presentation)
- all tokens should be qualified by properties
- you should lookup by existence of property

(still not having clear idea how to make it generic, only ad-hoc)

approach can be simulated by OOP expressivity, eg:

declare UK is country;
declare England is region [UK]
declare London is capital [UK], capital [England];
declare Wales is region [UK];
declare Cardiff is region [UK];

and then lookup via relation:
for each (region[France])



Sam
> --
> Website: http://www.illuminated.co.uk/
>
>
Re: Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?) [ In reply to ]
On 2024-01-03 1:17 a.m., Branislav Zahradník wrote:
> As mentioned in other thread, same arguments as for some (afaik most of) SQL
> coding standards uses singular for table name.

As I replied in the other thread, this is incorrect; most SQL coding standards
day plural for table names, singular for individual rows.

> You don't care what is name of collection, you do care what single element of
> collection represents.

Yes you care what a single collection element represents, but at the same time
when you give the collection as a whole a name, it needs to be something that is
appropriate for a collection of those individuals.

Saying "foreach person in people" reads better than "foreach person in person",
doesn't it? The latter seems rather confusing or nonsensical.

> 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), ...],
> );

As for naming hashes like these, there is an important thing that no one seems
to have raised yet, which is that they work best with a THIRD OPTION, as they
are neither scalars nor arrays in the general case.

For these hashes it is NEITHER plural nor singular, rather the name should
reflect the relationship.

See this altered version of your example:

my @all_cities = qw(London Cardiff Edinburgh Belfast);
my %for_each_country_its_capital = (
England => 'London',
Wales => 'Cardiff',
Scotland => 'Edinburgh',
'Northern Ireland' => 'Belfast',
);
my $capital_city = $for_each_country_its_capital{get_country()};

my %for_each_region_its_capitals = (
gb => [qw(London Cardiff Edinburgh Belfast)],
fr => [qw(Ajaccio Bordeaux Dijon Lille), ...],
);

-- Darren Duncan
Re: Variable naming (was: Re: Maybe it's time to disallow two variables with the same name and different sigil?) [ In reply to ]
>
> Saying "foreach person in people" reads better than "foreach person in
> person",
> doesn't it? The latter seems rather confusing or nonsensical.
>
>
.. and here I'm where I don't know how to express myself :-) Let's try:

you should not specify "in person" or "in people" at all. You should be
describing quality of persons
you want to iterate over.

Going back to cities (pseudocode):

foreach city { }
foreach regional_capital (country == France)

Regarding plural:
Iterators, generators, lookups - all of them may be plural.


>
> For these hashes it is NEITHER plural nor singular, rather the name should
> reflect the relationship.
>

With small modification this is probably best description: "For collections
..."