in reply to A matter of style

Note/Update: This was a response to dragonchilds reply, but I could not see the node. I apologize if this appears in the thread twice, but I didnt want it to get lost in the shuffle, so I moved it to a direct response
# Your code # Pure sub walk_and_do { my $self = shift; my $rc = 1; foreach my $child (@{$self->children}) { $rc = $child->walk_and_do; last unless $rc; } unless ($rc) { $rc = $self->do_my_stuff; } return $rc; } # Same function, single return, just as clear (for me) # ala TIMTOWTDI sub walk_and_do { my($self,$flag,@return); $self = shift; for ( @{$self->childer} ) { (last && $flag = "$_->{err}") unless $_->walk_and_do; } if ($flag) { @return = ('0', "$flag"); } else { @return = $self->do_my_stuff || ('0', $self->{err}); } return(@return); }# END sub walk_and_do
I don't want this to spiral downwards. What we are talking about here is PERSONAL style. For me its easier to maintain a single entry and single exit. Ill deviate when appropriate, but I tend to try and keep things straightforward. It makes it easier *for me* to maintain and extend my code.

/* And the Creator, against his better judgement, wrote man.c */

Replies are listed 'Best First'.
Re: Re: A matter of style
by jdporter (Paladin) on Feb 14, 2003 at 22:47 UTC
    Also, if one doesn't like normal, "deterministic" multiple exit points, then one must be horrified by the concept of exceptions.

    But when multiple exit is used in the case of error conditions, exceptions make things even cleaner.
    sub walk_and_do { my $self = shift; $_->walk_and_do for @{$self->children}; $self->do_my_stuff; # could throw an exception. } eval { $root->walk_and_do; };
    The fact that do_my_stuff could throw an exception means that any of the recursive calls to walk_and_do could exit early, via the exception. And that is as it should be.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.