From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,3f73d873d16dcda7 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Return statements and their scope - guideline Date: Wed, 14 Feb 2007 15:06:25 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1171483586 27227 192.74.137.71 (14 Feb 2007 20:06:26 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 14 Feb 2007 20:06:26 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:H4e/XEASe17pi+/W7WKe7LSfinI= Xref: g2news2.google.com comp.lang.ada:9329 Date: 2007-02-14T15:06:25-05:00 List-Id: Maciej Sobczak writes: > I have found a coding guideline that allows return statements only in > the outermost scope in the subprogram - which is supposed to avoid > obscuring the control flow. > > What is outermost scope in this context? That doesn't make sense if it's using the term "scope" in the Ada sense. Maybe it means "declarative region", which is defined in RM-8.1. Or maybe it just means the return statement can't be nested inside any other construct, which as Adam pointed out, means it has to be the last statement of the function. > function F return Integer is > begin > if Some_Condition then > return 0; -- (1) > end if; > -- ... > declare > X : Integer; > begin > -- ... > return X; -- (2) > end; > -- ... > return 7; -- (3) > end F; > > Is (1) above in the outermost scope? > I understand that (2) is not (and is therefore discouraged) and (3) is > definitely in the outermost scope, but (1) is not very obvious. What's the purpose of this coding convention? In terms of readability, I see no particular difference between (1) and (2). Both have the problem that the programmer might add some code at the end of the function (or procedure!), thinking it will be executed just before returning, but it won't. On possibility is to put a big fat comment on returns that are buried somewhere in the middle of the subprogram, like this: procedure Blah is begin ... 50 lines loop ... if ... then return; ---------------------------------------------------------------- end if; end loop; ... 50 more lines end Blah; Note that in Ada 2005 you can do things like this: function F return Integer is begin return Result: Integer := 123 do ... -- all the code of the function can go in here if ... then Result := Result + 1; ... end return; end F; - Bob