On Tue, Sep 20, 2005 at 08:58:41PM +0200, Juerd wrote:
> Yuval Kogman skribis 2005-09-20 20:33 (+0300):
> > Today on #perl6 I complained about the fact that this is always
> > inelegant:
> >     if ($condition) { pre }
> >     unconditional midsection;
> >     if ($condition) { post }
> I believe it's not inelegant enough to do something about.
> The unconditional part is easily copied if it's a single line, and can
> easily be factored to a method or sub if it's not. Especially with
> lexical subs.

There's a middle range where midsection is complicated enough
that copying it is a bad idea, but it is still sufficiently
short that factoring it into a sub causes more clutter and
interrupted thought patterns.

I've half-heartedly wished for this sort of linguistic construct
many times over the years, but it comes up rarely enough that
it's never been a burning desire.  Furthermore, any language
construct I've considered seems worse to me than just using
statement modifiers.  (So, this desire became far less of a
burning issue when I began programming in perl instead of C
and other languages.)

I don't like copying code of even a small amount of complexity -
sooner or later a change will happen to one copy that doesn't
get made to the other copy of the code, and a bug has been

So, I just assign the condition to a variable and use:

        my $cond = ... condition ...;

        pre             if $cond;


        post            if $cond;

When I read this sort of code to check it for problems, I
want clarity.  I'm going to have to read it twice to ensure
that it is correct for both of the ways it will be used
(when $cond is true and when it is false).  Moving mid into
a subroutine makes that reading harder.  Even the syntax that
Yuval suggests is more cluttered for reading the "straight-line"
code for the $cond is true case.

Reading the code for the case when $cond is false is easy no
matter which coding method you use - all of the midsection
code is in one place with any technique.

But when reading the code for the case when $cond is true,
I can read the code as if it were:




by simply ignoring the trailing " if $cond;" as I read.
Ignoring the text off to the right is easy; much easier than
ignoring syntax that is mixed into the code, or than reading
code that has been broken into 3 parts and the middle part is
somewhere on the page before or after the other two parts.


Reply via email to