comp.lang.ada
 help / color / mirror / Atom feed
From: "Adam Beneschan" <adam@irvine.com>
Subject: Re: Return statements and their scope - guideline
Date: 14 Feb 2007 08:46:37 -0800
Date: 2007-02-14T08:46:37-08:00	[thread overview]
Message-ID: <1171471597.672029.141200@v45g2000cwv.googlegroups.com> (raw)
In-Reply-To: <eqvcsh$ql3$1@cernne03.cern.ch>

On Feb 14, 8:20 am, Maciej Sobczak <no.s...@no.spam.com> 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







  parent reply	other threads:[~2007-02-14 16:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-14 16:20 Return statements and their scope - guideline Maciej Sobczak
2007-02-14 16:41 ` Jeffrey R. Carter
2007-02-14 17:31   ` Adam Beneschan
2007-02-15  7:33     ` Maciej Sobczak
2007-02-15  7:37   ` Maciej Sobczak
2007-02-14 16:46 ` Adam Beneschan [this message]
2007-02-14 20:06 ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox