[tpm] hash naming convention: singular or plural

Alex Beamish talexb at gmail.com
Fri Aug 8 07:46:42 PDT 2008


On Fri, Aug 8, 2008 at 11:38 AM, Alexander Anderson
<a.anderson at utoronto.ca> wrote:
> Hello Toronto Perl Mongers,
>
> Names of scalars are usually singular, for example,
>
>  $month = 'Aug';
>
> Names of arrays are usually plural, for example,
>
>  @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
>
> How do you name your hashes, for example,
>
>  %months = ( Jan => 1, Feb => 2, ..., Dec => 12 );
>  or
>  %month = ( Jan => 1, Feb => 2, ..., Dec => 12 );

As arrays and hashes are usually used to contain multiple elements, I
usually use the plural of the word for the variable names. However,
that's only 'usually' -- if the hash is actually an object with a
variety of attributes, then I would use a singular word. So I'd use

  $months{'Jan'}

but

  $config{'httpd_dir'}

Just run an eye over some code and see if it's as clear as possible
before writing it out. If it looks a little weird or cute, it's
probably safer (if a little more boring) to clean it up.

Remember, the worst thing that can possibly happen is looking over
some code you wrote six months ago and being unable to parse what the
heck is going on. That's embarassing.

Alex

-- 
Alex Beamish
Toronto, Ontario
aka talexb


More information about the toronto-pm mailing list