SPUG: Self-generating code: yea or nay? (was: Dijsktra is Dead)

Richard Anderson richard at richard-anderson.org
Fri Aug 9 00:25:35 CDT 2002


Speaking of unstructured coding, I recently updated the online grep/map/sort
tutorial and added the example below.  This got me thinking: are there any
cases where self-generating code is "better" (in some sense) than the
alternatives?  I feel the answer is no - can anyone provide a
counter-example?

Find prime numbers: a cautionary tale

Lastly, an example of how NOT to use map. Once you become proficient with
map, it is tempting to apply it to every problem involving an array or hash.
This can lead to unfortunate code like this:


foreach $num (1 .. 1000) {
    @expr = map { '$_ % ' . $_ . ' &&' } 2 .. int sqrt $num;
    if (eval "grep { @expr 1 } $num") { print "$num " }
}

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 ...

This works, but the code is such an evil mess that it made my dog cry.
Self-generating code is inherently difficult to read, debug and modify.

Look at how easy it is to understand a straightforward implementation of the
same algorithm:
CANDIDATE:
    foreach $num (1 .. 1000) {
        foreach $factor (2 .. int sqrt $num) {
            unless ($num % $factor) { next CANDIDATE }
        }
        print "$num ";
    }

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 ...
As a bonus, the simple implementation is two orders of magnitude faster than
the self-generating code! In general, the simpler your code looks, the more
the compiler can optimize it. Of course, a complex implementation of a fast
algorithm can trump a simple implementation of a slow algorithm.

Now that we have gazed upon the Dark Side, let us return to the path of
righteous, simple code ...

(Complete tutorial at
http://www.raycosoft.com/rayco/support/perl_tutor.html )

Cheers,
Richard
richard at richard-anderson.org
www.richard-anderson.org
www.raycosoft.com
----- Original Message -----
From: "Asim Jalis" <asimjalis at yahoo.com>
To: <spug-list at pm.org>
Sent: Thursday, August 08, 2002 2:51 PM
Subject: SPUG: Dijsktra is Dead


> Dijsktra is dead. We can all write unstructured
> code again.
>
> <http://www.austin360.com/statesman/editions/thursday/news_5.html>
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      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
>
>



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