A question of Style, was: SPUG: Sort an array question

Peter Darley pdarley at kinesis-cem.com
Fri Dec 27 13:31:49 CST 2002


	I disagree with this.  I think that index1 and index2 are preferable to i
and j.  For one thing, i and j may be commonly used by 90% of people to
represent indexes to arrays, but why not include something that is going to
be immediately obvious to the other 10% that have some different convention
that they use?

	The algorithm could be described as "Step through the array in order and
compare the working value with the values in the array that occur after it,
switching them if the compared value is lower than the working value", which
leads us to the variable names of $Working and $Compare.  Why not do
something like:

	for my $Working ( 0 .. $#Array) {
	    for my $Compare ( $Working + 1 .. $#Array) {
		$Temp = $Array[$Compare] . $array[$Working];	  # join compare elements

	With operators where order makes a difference (like the . operator) it has
an even bigger impact on readability, because instead of having "some
element of an array . some other element of an array" we have "element of
the array that we're comparing . element of the array that we're working
with".  I think this adds a lot to the readability.

	I've changed the variables to initial caps, since it helps them stand out
from all the lower case reserved words.  This is entirely individual

	Anyway, as y'all can probably tell, this is a pet peeve of mine.  I've put
out my opinion and so I'll drop it now. :)

Peter Darley

-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
Brian Hatch
Sent: Friday, December 27, 2002 10:35 AM
To: Peter Darley
Cc: William Julien; blueovalfun at yahoo.com; spug-list at pm.org
Subject: Re: A question of Style, was: SPUG: Sort an array question

> 	In my opinion one of the biggest things that promotes readability is the
> use of long variable names.  Since when I program I spend far more time
> sitting and thinking about how I want the program to work than I do
> typing things in, using short variable names ($i, etc.) seems like
> optimizing for speed of typing over ease of programming (readability).
> seems a bit backward to me.

This is true in general, but there are many cases where a short
name is a good choice.  $i or $j are commonly used to indicate
'index into an array', and that's just what was used in the example

	for( $i = 0; $i <= $#array; $i++ ) {
	    for( $j = $i+1; $j <= $#array; $j++ ) {
		$tmp = $array[$j] . $array[$i];	  # join compare elements

You could call them 'index1' and 'index2' but that actually makes
the code less readable, as the name takes up too much space
to make it harder to read the 'jist' of things - comparing
values inside @array.

I have no problem with short names when they have a very short
life expectancy.  For example I'd have written the above as

	for my $i ( 0 .. $#array) {
	    for my $j ( $i+1 .. $#array) {
		$tmp = $array[$j] . $array[$i];	  # join compare elements

Why?  $i and $j are scoped only inside this block.  No short names
should be globally scoped - makes it too hard to remember what
they're used for.  (It's also more readable than the traditional
3-part for loop.  Yes, the above is actually a foreach loop,
but I am too lazy to type the four extra characters if perl
doesn't care.)

Brian Hatch                  "That was the law, as set down by Valen.
   Systems and                Three castes: worker, religious, warrior.
   Security Engineer          They build, you pray, we fight."

Every message PGP signed

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list