First of all I agree with the various thoughts and points you make. So let me just comment on the example.

I see what you mean. Aesthetically I don't like the exit condition being woven through the logic, what happens if some day you want to have this cache updating be part of a longer-running script? Also reading the logic through I saw a lot of combinations of actions being taken, but it was not at all obvious why some of them were. And the nesting is pretty darned deep.

I used an indicator variable to do the actions and restructured. Comments?

{ ## main loop my $cache = 'current'; # Hope for the best if (not cache_is_good()) { $cache = 'stale'; # Changed expectations if (i_am_the_writer()) { if (cache_is_stale()) { unless (start_background_update()) { $cache = 'current'; # Gotta do it foreground } } else { $cache = 'current'; # Have nothing to show, gotta do it } } elsif(not cache_is_stale()) { # Stuck, gotta retry close_cache(); sleep 5; redo; } update_cache() if $cache eq 'current'; # Make it so } show_cache($cache); } sub start_background_update { # NOTE: Uses implicit returns. if (i_can_fork()) { unless (i_am_the_parent()) { be_a_child(); update_cache(); exit 0; } } }
UPDATE
Yeah, I know. I was using implicit returns without a comment. At the time it made sense, but that is the kind of thing I rethink and mention shortly after...

In reply to RE (tilly) 1: On elegant coding... by tilly
in thread Just thinking ... by toadi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.