* Re: Return statements and their scope - guideline
2007-02-14 16:20 Return statements and their scope - guideline Maciej Sobczak
2007-02-14 16:41 ` Jeffrey R. Carter
@ 2007-02-14 16:46 ` Adam Beneschan
2007-02-14 20:06 ` Robert A Duff
2 siblings, 0 replies; 7+ messages in thread
From: Adam Beneschan @ 2007-02-14 16:46 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Return statements and their scope - guideline
2007-02-14 16:20 Return statements and their scope - guideline Maciej Sobczak
2007-02-14 16:41 ` Jeffrey R. Carter
2007-02-14 16:46 ` Adam Beneschan
@ 2007-02-14 20:06 ` Robert A Duff
2 siblings, 0 replies; 7+ messages in thread
From: Robert A Duff @ 2007-02-14 20:06 UTC (permalink / raw)
Maciej Sobczak <no.spam@no.spam.com> 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
^ permalink raw reply [flat|nested] 7+ messages in thread