[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] The Powers of Annoy



miku <miku@onlinehome.de> wrote:

> Sure itıs a classic. But as sure as my first program is helloing the world,
> my second program as a learner (in this case, MacPerl) rearranges disks on
> sticks. Now I would like to "improve" :) my programming style, i. e. learn
> how make the program even *more compact* and shorter, or even *trickier*. Do
> you have any suggestions?

This is a very BAD approach to programing style.  Entirely wrong-headed and deeply in need of attitude adjustment.  Rather than attempt to make your code more compact and "trickier", strive instead for structure, elegance, and readability.  perlstyle.pod is an excellent starting point, although I don't agree with TomC in all the particulars.  If you code the way he suggests, I can guarantee the no one will ever complain.  Conversely, post more code like your current example, and rather than complain, I'll just start sending you copies of my optometry bills. I'm sure that inducing that amount of eyestrain violates several workplace safety protocols as well! ;-)

Below, I haven't made any actual changes to your syntax.  You need to carefully consider your reasoning for changing things like the list seperator.  It doesn't look to me like you're doing anything that *demands* it.  You're going through some unecessary gyrations with assigning values to scalars, which might make sense in the context of a larger program, but here just serve to fill up space.  Is it really more compact to assign "length($f)" to $fl, rather than just using "length($f)" where you need to?  Conversely, if you're going to use them, try giving your variables MEANINGFUL names!  Remember, life is not an obsfucated Perl contest! I would suggest both -w and "use strict" as excellent habits.  Declaring one's variables up front is just a good idea in general.  In particular, if you plan on keeping your code, rather than just tossing it away, the "strict" pragma will save you a ton of maintanance headaches down the road.

#!perl

$f = "54321"; 
$fl= length($f); 
$e = 2**$fl-1; 
$" = ", ";
@o = ($f, "", "");

print "\n\nThe Towers of Annoy\n\nInitial state: @o\n";

$y=0;
$n=0; 

until ($n >= $e) {
    chop(@o[$y]);  # Scalar value @o[$y] better written as $o[$y].
    $u = $y;
    $y=(($y+1)%3);
    $o[$y]
    .= "1";
    $n++;
    print "Move #$n: @o\n";
    unless ($n >= $e) {
        $p = @o[$u];
        $s = length($p);
        $g = substr($p,$s-1,1);
        $v = (3-($y+$u));
        $r = @o[$v];
        $t = length($r);
        $h = substr($r,$t-1,1);
        if ($h eq "") {
            chop(@o[$u]);
            @o[$v] .= $g
            }
        elsif (($g eq "") || ($h lt $g)) {
            chop(@o[$v]);
            @o[$u] .= $h
            }
		      else 
            {
            chop(@o[$u]);
		         	@o[$v] .= $g
		    	 }
        $n++;
        print "Move #$n: @o\n";
        }
    }

__END__

The above is much more plesant on the eye, and the white-space effects the efficiency of the code not one whit at all.  It's still fairly opaque, unless you happen to have written it.  Please note my comment on Line 15 fo the code.  You do this in several places, and -w would tell you about it if you used it.

Apply programmer hubris, and adopt the belief that all your code is destined for immortality.  Believe that others ADORE reading your code, and LOVE maintaining it, and then write code as if that were the literal truth.

--B

Brian McNett, Webmaster
*************************************************************
Mycoinfo. The world's first mycology e-journal.
http://www.mycoinfo.com/
*************************************************************


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org