[Pdx-pm] Ugly code!

Michael R. Wolf MichaelRunningWolf at att.net
Sat May 31 15:44:17 CDT 2003


Ovid <poec at yahoo.com> writes:

> --- Austin Schutz <tex at off.org> wrote:
>> 	Just out of curiosity, what's the ugliest code you've ever seen in
>> the wild (no names attached, to protect the guilty)?
>
> Coincidentally, I was working on some of that yesterday:
>
>   sub connect {
>       my $self = shift;
>       return $self;
>   }
>
> I was pulled off of that project before I could fix this and similar
> issues. The code was full of little gems like that. It dawned on me
> that I could have fun and do stuff like:
>
>   my $object = $object->connect->connect->connect->connect->connect;
>
> (It's effectively a no-op)

Actually, it's an identity.

I couldn't think of any reason it's useful except to stub out a
function during prototyping.

sub remove_duplicates {
   my $self = shift;
   # Code should go here...
   return $self
}

$object->remove_duplicates->print;

MJD, on the other hand, *has* thought of how it can be useful in a
production system. I happened to stumble across, and read, a talk of
MJD's. I thought it was interesting.  See

    http://perl.plover.com/yak/identity/ 

        Description

        [I] gave this bizarre talk at TPC 2001 in San Diego. The title
        backfired: Too many people didn't know what the identity
        function was, and those who did know assumed that I must have
        been referring to something else. The identity function is a
        function such as 
            sub identity { return $_[0] } 
        which returns its argument unchanged. In this talk, I show
        four unexpectedly useful uses for the identity function

Having read the talk, which uses Perl syntax to illustrate the
concepts, I was reviewing some emacs lisp code that used the
(built-in) identity function in some map (similar to the map in Perl),
mapc, or mapconcat construct. Having read about it in MJD's Perl talk
helped me understand the lisp construct a bit more.

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




More information about the Pdx-pm-list mailing list