comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Return statements and their scope - guideline
Date: Wed, 14 Feb 2007 15:06:25 -0500
Date: 2007-02-14T15:06:25-05:00	[thread overview]
Message-ID: <wccbqjw1p32.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: eqvcsh$ql3$1@cernne03.cern.ch

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



      parent reply	other threads:[~2007-02-14 20:06 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
2007-02-14 20:06 ` Robert A Duff [this message]
replies disabled

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