Variable Naming [was Re: [Israel.pm] catching $a, $b unnecessary my and maybe other things]

Shlomi Fish shlomif at iglu.org.il
Mon Jun 7 00:10:04 PDT 2004


On Sunday 06 June 2004 22:16, Omer Zak wrote:
> Shlomi Fish wrote:
> > Well, naming variables is something that requires some thinking. For
> > once, I disagree that giving variables long names is a healthy practice,
> > just because your editor has auto-completion. You can't rely on the
> > editor having that (joe, classic vi, etc.), and extremely long variable
> > names make the code more difficult to read because less code can be fit
> > on one line.
>
> I disagree with the belief that short variable names are better.
> When you use long names, you can grep for all occurrences of a variable
> in your code.
>

You can do it also for short variable names. Just use the word-boundary 
zero-length assertion. It's "\b" in perl5 regexps (perldoc perlre) and "\<" 
and "\>" in vim. Completely a non-issue.

> This is very useful during maintenance to ensure that you miss nothing
> when you make systematic updates.
>
> When you don't have too many false positives when grepping for a
> variable, you can use that grep to locate all places, which need to be
> modified as long as that variable is associated in some way with those
> places.
>
> If you use 'i', then you would have too many false positives.

Generally, I think a variable name should be meaningful, and unique within its 
scope, but not too long.

This code:

<<<
for($word_within_sentence_index=0; 
$word_within_sentence_index<$num_words_within_sentence;
$word_within_sentence_index++)
{
	# do something with $word_within_sentence_index
}
>>>

Is horrid. (even Joel Spolsky warns about it in his "guerilla guide to 
interviewing", saying that no sane experienced programmer will ever choose 
such a name). 

In that case, $word_index or $word_idx is just as adequate. 

Regards,

	Shlomi Fish

>                                               --- Omer
> My own blog is at http://www.livejournal.com/users/tddpirate/
>
> My opinions, as expressed in this E-mail message, are mine alone.
> They do not represent the official policy of any organization with which
> I may be affiliated in any way.
> WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://www.perl.org.il/mailman/listinfo/perl

-- 

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://shlomif.il.eu.org/

Quidquid latine dictum sit, altum viditur.
        [Whatever is said in Latin sounds profound.]



More information about the Perl mailing list