in reply to On elegant coding...
in thread Just thinking ...
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?
UPDATE{ ## 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; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE (tilly) 1: On elegant coding...
by merlyn (Sage) on Oct 14, 2000 at 08:03 UTC | |
by tilly (Archbishop) on Oct 16, 2000 at 17:00 UTC |