develooper Front page | perl.perl5.porters | Postings from July 2020

Re: try/catch and CLEANUP/FINALLY/&c.

Thread Previous | Thread Next
From:
Felipe Gasper
Date:
July 18, 2020 11:24
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
1B344721-28DD-40CA-A2B6-760BD9F2A0A3@felipegasper.com


> On Jul 18, 2020, at 3:00 AM, Branislav Zahradník <happy.barney@gmail.com> wrote:
> 
> 
> 
> On Fri, 17 Jul 2020 at 16:30, Felipe Gasper <felipe@felipegasper.com> wrote:
> 
> 
> > On Jul 17, 2020, at 10:22 AM, Paul LeoNerd Evans <leonerd@leonerd.org.uk> wrote:
> > 
> > On Fri, 17 Jul 2020 10:18:06 -0400
> 
> 
> Possibly, but then if I or someone else adds logic after the catch block, the CLEANUP won’t fire until after that. Consider:
> 
> =====
> show_notification('Frobnicating ...');
> CLEANUP { hide_notification() }
> 
> try {
>   attempt_frobnification();
>   report_success();
> }
> catch ($e) {
>   report_failed_frobnification($e);
> }
> 
> blocks_for_10_seconds();
> 
> 
> In such case you are free to use explicit block (using lexical-catch variant) (you should wrap that into  dedicated method anyway)
> 
> show_notification(...);
> {
>     CLEANUP { hide_notification };
>     CATCH { report_failed... }
> 
>     atempt_...
>     report_success
> }
> block_for_10_seconds

Right, but that extra code block requires extra diligence, which I think we all know oftentimes doesn’t happen. It’ll be yet another “shin-bumper” for new Perl initiates that discourages further use of the language.

> Lexical cleanup and catch blocks are more powerful than just plain try/catch/finally,
> 
> Ability to specify actions on exit before actual exit happens notifies the reader (maintainer after 10 years ...)
> that there is something "before" its eyes catch some exit point.

This assumes that the reader notices the CLEANUP and CATCH code block, which isn’t a forgone conclusion given that those blocks exist at the same indentation level as the rest. Again, it requires extra diligence to avoid mishandling.

> Plain cleanup is also a little bit less confusing than try/finally construct (there is try, where is catch?)

I disagree; the more widespread “finally” pattern is IMO much less confusing. Reasons:

1) The finally is declared after, and runs after, its preceding logic. CLEANUP is declared before but executes after, which is backward from the usual pattern that things declared later happen later.

2) In large code blocks I could easily miss a CLEANUP by not having scrolled up. And oy, what happens if a CLEANUP is declared *midway* through such a mess?!? That’s less likely to be problematic with try/catch/finally because the indentation guides comprehension of the flow.

3) Given the overwhelming precedent of try/catch/finally, a new Perl developer--likely to come from a different language, maybe resentful of having to maintain some dot-com-era code rather than “modernizing” it to Python--will more likely grasp try/catch/finally at first glance, whereas the more esoteric CLEANUP takes extra study.

> 
> (imho less powerful) try/catch/finally syntax can be still introduced by external module with rewriting:
>    try TRYBLOCK catch CATCHBLOCK finally FINALLYBLOCK
> into
>   { CATCH CATCHBLOCK  CLEANUP FINALLYBLOCK TRYBLOCK }

Are there other languages that implement this pattern? How popular are these languages?

The “composability” of multiple CATCH/CLEANUP blocks is kind of cool, but it would seem to facilitate long blocks of code, which I think most experienced developers--of whatever language--regard as less desirable (all things else equal) than small, simple blocks of logic.

Or what am I missing? Why do newer languages like Julia favor plain try/catch/finally rather than this more esoteric pattern? Given overwhelming precedent--and we can add C++ to the list if CATCH blocks are under consideration--for what it’s worth I feel like there ought to be more compelling reasons for Perl to dance to *quite* such a different beat than I’ve seen thus far.

-F
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About