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!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Ada and "early return" - opinion/practice question Date: Mon, 15 Mar 2021 19:37:02 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 15 Mar 2021 18:37:02 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="f78ab394426f4fd02a5b1ee9aa5ad963"; logging-data="10041"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZU6qfreUknfyWiXIA+OmNaEai8r+pWrc=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 Cancel-Lock: sha1:1rWDRJZAZK6OXvjG0pU6woZqqjk= In-Reply-To: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61534 List-Id: On 3/15/21 5:46 PM, 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? Other than the use of exceptions rather than a return code, this is a standard idiom in Ada. It's much easier to read and understand than the Pascal approach, just as a "loop and a a half" is much clearer with an exit than the Pascal approach. I seem to recall Robert Dewar arguing for this style on here many years ago. -- Jeff Carter "Choose a data representation that makes the program simple." Elements of Programming Style 188