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

Michael R. Wolf MichaelRunningWolf at att.net
Fri Dec 27 16:40:18 CST 2002

"Peter Darley" <pdarley at kinesis-cem.com> writes:

> Brian,
> 	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

Let's compare "necessary complexity" versus "unnecessary complexity".
I tend to use $i and $j when they're not really important; they're
unnecessary complexity to the problem at hand.  But, as Brian points
out, they may be necessary complexity (or at least useful touchpoints
back to the requirements), so I'd be inclined to agree with him on the
$compare and $working indices (although my preference is to avoid the
capitalizations -- the sigils serve that purpose for my eyes).

But, the whole idea of the index is *unnecessary complexity*.  It's
only purpose is to get the element that we care about.  We'd need it
if we were working in C or C++, but this is Perl -- we don't need the
index to get the element.  If the only reason for the index is to get
the element, don't use an index.  Since this loop bases its inner loop
index on the outer loop index, it's needed, therefore *necessary
complexity*.  But most nested loops don't work that way, and I'd posit
that the following code, which removes the index, is more readable,
having removed the unnecessary complexity.

 	for my $Working ( @Array) {
 	    for my $Compare ( @Arry ) {

Brain teaser:  Could I rewrite the loop as intended without an index?
Yes.  I'm not sure if I like it any better.  It seems to hide the
(relatively rare) difference from the normal idiom, that the inner
loop doesn't traverse the same bounds as the outer loop.

for(my @working = @array; @working; undef) {
    my $working = shift @working;

    for(my @compare = @working; @compare; undef) {
        my $compare = shift @compare;

        $temp = "$compare$working";

I've eliminated indices to access array elements, but I've also
introduced other scalar variables.  Remove two, add two.  Different!

Is this any better?

for(my @working = @array; my $working = shift @working; undef) {
    for(my @compare = @working; my $compare = shift @compare; undef) {
        $temp = "$compare$working";

I don't think so.  Having tried to eliminate the *unnecessary*
complexity of the indices, I find that they help me more in this
problem where it's not a complete iteration over the $i/$j pairs.

Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net

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