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!postnews.google.com!v45g2000cwv.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Return statements and their scope - guideline Date: 14 Feb 2007 08:46:37 -0800 Organization: http://groups.google.com Message-ID: <1171471597.672029.141200@v45g2000cwv.googlegroups.com> References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1171471611 16223 127.0.0.1 (14 Feb 2007 16:46:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 14 Feb 2007 16:46:51 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: v45g2000cwv.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news2.google.com comp.lang.ada:9325 Date: 2007-02-14T08:46:37-08:00 List-Id: On Feb 14, 8:20 am, Maciej Sobczak wrote: > Hi, > > 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? > > 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. I don't think your question can be answered satisfactorily by referring to the Ada manual. The term "scope" is discussed in 3.1(8), but it refers to the scope of a declaration. The scope of the declaration of X would be more or less the DECLARE block in which it's declared. But it's not really satisfactory to base a definition on that, because it would mean that a block that declares identifiers would be a scope, but a block that doesn't (i.e. with no DECLARE keyword) wouldn't---and this doesn't make much sense. As far as I can tell, the RM never talks about a "scope" without it being the scope "of" something---the scope of a declaration, or of a USE or WITH clause, or maybe some other things. So you'll have to look elsewhere. One thing to note is that if RETURN is not allowed inside any compound statement (such as an IF, in your example above), then there's no point in putting it anywhere except at the end of the subprogram, because if a RETURN statement is not performed conditionally then everything after it is dead code. Anyway, from the standpoint of "coding guidelines"---which really means from the standpoint of "writing it in a way so that the next person who reads this is less likely to have a misunderstanding"---I would think that the main reason for not putting RETURN higher up in the code is that if you do, and if it's a fairly sizable subprogram, someone might look at a statement toward the end of the subprogram and miss the fact that the statement might not always be executed because of a RETURN statement that occurred earlier. That's just my opinion; I can't think of any other reason why an earlier RETURN, even a nested one, might cause a problem. And probably, it would cause more of a problem for such a RETURN to be nested inside a block than otherwise. That's how I'd think about it, and IMHO that's how you should answer the question with respect to the real-world code you're thinking about---an abstract example such as the one you've posted isn't going to help answer your question. Hope this helps. -- Adam