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!news.mb-net.net!open-news-network.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: Mon, 15 Mar 2021 18:02:09 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> NNTP-Posting-Host: 5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61525 List-Id: On 2021-03-15 17:46, John McCabe wrote: > I hope this isn't a FAQ (it's hard to find relevant articles) but can someone guide me on the 'normal' treatment in Ada style of what appears to be referred to (by C/C++ programmers) as early-return. > > 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've probably mentioned this before, but it's a long time since I used Ada in anger and I don't remember seeing stuff like that when I did use Ada a lot; does anyone write stuff like that in Ada? > When I first learnt to program properly it was using Pascal with, as I remember it, only one return from a function being allowed, so over the years I've mostly looked at positive conditions and indented stuff, pulling the stuff in the middle out into its own procedure or function where appropriate, but you see so many people defending this style in C/C++ that I wonder whether it really is defensible? I see nothing wrong with it. In Ada: function fn () return is begin if then return ; elsif then return ; elsif then return ; else --Only get here if everything's good... return ; end fn; P.S. The old mantra of structured programming was one entry, one exit. This is why some argued for single return while storing result code in a variable. Clearly adding a result variable would reduce readability rather than improve it. P.P.S. One could debate exception vs. return code, but this is another story for another day. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de