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=0.8 required=3.0 tests=BAYES_50 autolearn=ham autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:a05:6214:d84:: with SMTP id e4mr11936548qve.26.1615829352723; Mon, 15 Mar 2021 10:29:12 -0700 (PDT) X-Received: by 2002:a25:6f41:: with SMTP id k62mr218190ybc.253.1615829352535; Mon, 15 Mar 2021 10:29:12 -0700 (PDT) Path: eternal-september.org!reader02.eternal-september.org!news.uzoreto.com!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 15 Mar 2021 10:29:12 -0700 (PDT) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=80.229.20.55; posting-account=WsVe0AoAAABheGmBjlLgPWhgIw6kxcL6 NNTP-Posting-Host: 80.229.20.55 References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <53b2508a-b005-4335-b069-810249dc6cd1n@googlegroups.com> Subject: Re: Ada and "early return" - opinion/practice question From: John McCabe Injection-Date: Mon, 15 Mar 2021 17:29:12 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:61526 List-Id: On Monday, 15 March 2021 at 17:02:09 UTC, Dmitry A. Kazakov wrote: > On 2021-03-15 17:46, John McCabe wrote:=20 > > I hope this isn't a FAQ (it's hard to find relevant articles) but can s= omeone guide me on the 'normal' treatment in Ada style of what appears to b= e referred to (by C/C++ programmers) as early-return.=20 > >=20 > > For example, you have a C++ function (pseudo code sort of thing):=20 <..snip..> > I see nothing wrong with it. In Ada:=20 >=20 > function fn () return is=20 > begin=20 > if then > return ; > elsif then=20 > return ;=20 > elsif then=20 > return ;=20 > else --Only get here if everything's good... > =20 > return ; > end fn;=20 Thanks for that Dmitry; the subtlety here is the use of elsif thoughout tho= ugh, which tends to be skipped in C++ sources. Put that way, what I origina= lly wrote would be: fn() { if () { return ; } else if () { return ; } else if () { return ; } else { // Only get here if everything's good... return ; } } I probably wouldn't mind that layout so much, since it makes it clear that = the code that's done at the bottom only happens when all the other conditio= ns have been satisfied, and you're less likely to get some random behaviour= because of a missing return in the bunch of earlier tests.