SPUG: Debugging methodology (was: How to deal with same name packages?)

Michael R. Wolf MichaelRunningWolf at att.net
Tue Jan 29 09:52:20 CST 2002


Tim Maher <tim at consultix-inc.com> writes:

> On Mon, Jan 28, 2002 at 03:08:23PM -0800, Colin Meyer wrote:
[...]
> >   Or even:
> >  
> >        print (STDERR "Debug statement"), second_thing if $debug > 1;
> 
> That's just too kinky. I thought the whole idea of this backwards
> statement was to make the code *easier* to read, not to make it
> harder to see how much is being executed conditionally!

Kinky?  Maybe.  Pushing the edge of my one-line limit?
Probably.

As with all formatting and logic issues in programming,
there are competing values toward a balance point.

- Don't waste extra curlies for a single-expression block
- Hilight control flow
- Hilight normal execution
- Hilight normal execution
- Limit expression modifiers to a single line

For example, I think the inverted control flow works better
in the first example below.   The extra curlies, whitespace,
and punctuation do not add any readability or flexibility in
the second one.

use Memoize;
memoize (fib, fluffy_fib);

sub fib
{
    $n = shift;
    return 1 if $n == 1;
    return 0 if $n < 1;
    return fib($n -1) + fib($n-2);
}

sub fluffy_fib
{
    $n = shift;
    if ($n == 1) {
	return 1;
    }
    if ($n < 1) {
	return 0;
    }
    return fluffy_fib($n -1) + fluffy_fib($n-2);
}

And here, I like that the control flow is easy to see.  I
over-emphasized it by lining up the "if", and it still fits
on a line nicely.  Even when there is something to do before
next-ing, like the pragma processing.

sub doit {
    while(<>) {
	next                if /^\s*$/;  # skip blank lines
	next                if /^\s*#/;  # skip shell-style comments
	do_pragma($_), next if /^pragma\b/;
	doit_toit($_);
    }
}

Obviously kinky, perverse.

do {
  many lines of code here
  ..
  ..
  ..
  } if condition


So I agree with your sentiment.  I choose a different
balance point for short expressions, especially if they tend
to draw attention to the important part of the clause.

Enjoy, it's Perl!
It's Perl, enjoy!

There's a different focus to the following (equivalent)
statements.  I utilize this in my English.  And in my Perl.

enjoy if $is_perl;

if ($is_perl) {
  enjoy;
}


-- 
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://zipcon.net/spug/





More information about the spug-list mailing list