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

Richard Anderson richard at richard-anderson.org
Mon Aug 12 15:15:16 CDT 2002


Hi Colin.  Your example of Hook::LexWrap, Marc M. Adkins example and
Geoffrey & Kristin Grosenbach's example of generating Java
database-layer code are reasonable counter-examples, and and illustrate how
easy Perl makes to do things that would be difficult in other languages.  I
think I'll tone down the tutorial's condemnation of self-generating code.
However, Colin's comment about
> ...see Lex::HookWrap. This code may take some studying (or a lecture from
Damian ;-) to understand. Lex::HookWrap
> autogenerates a class to unhook the hooks, when a scope is left (causing
> an object to go out of scope, and its DESTROY method to be called).
> What's the alternative to autogenerated code here? ;-) It's doable, but
> with a DESTROY method that is much thicker and less intuitive.

is indicative that self-generating code can be kind of dodgy.

Cheers,
Richard
richard at richard-anderson.org
www.richard-anderson.org
www.raycosoft.com
----- Original Message -----
From: "Colin Meyer" <cmeyer at helvella.org>
To: "Richard Anderson" <richard at richard-anderson.org>
Cc: "spug" <spug-list at pm.org>
Sent: Friday, August 09, 2002 10:12 AM
Subject: Re: SPUG: Self-generating code: yea or nay? (was: Dijsktra is Dead)


> On Thu, Aug 08, 2002 at 10:25:35PM -0700, Richard Anderson wrote:
> > 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?
> [...]
> >
> > 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.
>
> This example is difficult to read, but that doesn't demonstrate that
> all code generated by code is so messy.
>
> Consider the code it generates when $num == 10:
>
> @expr will be: ( '$_ % 2 &&', '$_ % 3' )
>
> the eval'd string will be: "grep { $_ % 2 && $_ % 3 && 1 } $num".
>
> Just because you chose to generate code that is odd to the typical
> Perl programmer doesn't mean that all generated code need be so odd.
>
> >
> > Look at how easy it is to understand a straightforward implementation
> > of the same algorithm:
>
> Comparing apples and oranges doesn't lend to useful conclusions.
>
> For a useful and understandable example of autogenerated code, take a
> look at something like Class::Struct (or Chapter 8 of _OO Perl_ for a
> nice analysis).
>
> A module like this demonstrates when when autogenerated code is better
> than the alternative.  It is a waste of time for the programmer to
> repeatedly hand code simple accessors, yet how many of us do so on a
> regular basis?  Why not let automate the task and let a program generate
> the code for you?
>
> For a very useful example, see Lex::HookWrap. This code may take some
> studying (or a lecture from Damian ;-) to understand. Lex::HookWrap
> autogenerates a class to unhook the hooks, when a scope is left (causing
> an object to go out of scope, and its DESTROY method to be called).
> What's the alternative to autogenerated code here? ;-) It's doable, but
> with a DESTROY method that is much thicker and less intuitive.
>
> As with many features of Perl, don't use autogenerating code, if you
> aren't comfortable with it.  When writing code that others will be
> maintaining, consider their capabilities and comfort as well.
>
> Have fun,
> -Colin.
>


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