From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Ada and "early return" - opinion/practice question Date: Tue, 16 Mar 2021 10:46:13 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> <86eegge32o.fsf@stephe-leake.org> NNTP-Posting-Host: 5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: reader02.eternal-september.org comp.lang.ada:61555 List-Id: On 2021-03-16 09:24, John McCabe wrote: > On 15/03/2021 17:31, Stephen Leake wrote: >> John McCabe writes: >> > <..snip..> > >>> For example, you have a C++ function (pseudo code sort of thing): >>> >>> fn() >>> { >>>     if () >>>     { >>>         return ; >>>     } >>> >>>     if () >>>     { >>>         return ; >>>     } >>> >>>     if () >>>     { >>>         return ; >>>     } >>> >>>     // Only get here if everything's good... >>>     >>>     return ; >>> } >>> > > > >> I would tend to use an exception for "something bad", but that depends >> on the overall design. > > Thinking back on this point, would you do it a style where you try to do > what needs to be done then handle the exception when it fails? For example, > in this style > > begin >    > exception >    when blah blah > end > > Or do you mean, rather than return when the undesirable condition > occurs, do > something like: > > if then >    raise > end if; > > In other words, does each undesirable condition, when it occurs, throw a > [potentially] different exception? It is a difficult choice. One would expect a different exception for each case. Unfortunately Ada lacks important features to support that design: 1. Exception contracts. If you have many different exceptions it becomes increasingly difficult to track them. 2. There is no way to group exceptions in a tree-like or relational structure. You cannot name a group and handle its exceptions as whole. You have no operations on such groups etc. A rather pragmatic design is to use a minimal set of exceptions packing structured information into the exception message. A quite fragile approach. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de