[Oberon] Eliminating RETURN statements
hklaver at dds.nl
Wed Apr 8 18:14:27 CEST 2020
Thanks very much for your reply.
>> (* Oberon-07, right output *)
>> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
>> VAR k: INTEGER; OK: BOOLEAN;
>> k := 0; OK := TRUE;
>> WHILE (k <= piecemax[i]) & OK DO
>> IF p[i][k] THEN
>> IF puzzl[j + k] THEN OK := FALSE END
>> RETURN OK
>> END Fit;
> I prefer something that clarifies that there are actually two terminating conditions but a different result for each. For example, something like:
> (* Untested *)
> PROCEDURE Fit (i, j: INTEGER): BOOLEAN;
> continue = 0;
> failed = 1;
> terminated = 2;
> VAR k, status: INTEGER;
> k := 0;
> IF k > piecemax[i] THEN status := terminated
> ELSIF p[i][k] & puzzl[j + k] THEN status := failed
> ELSE INC(k); status := continue
> UNTIL status # continue
> RETURN status # failed
> END Fit;
Your code works great.
It has a magical aspect, because the 'status := terminated' seems to be superfluous: 'terminated' is never tested explicitly, but the assignment still is essential because it is tested implicitly!
>> Maybe the Oberon-07 compiler should give a *warning* and not an error message
>> if a function procedure has more RETURN statements than one single RETURN at
>> its end. Then legacy code could be ported more easily and more clearly to the
>> latest version of the language.
> Aargh! No, no, no! That reminds me of a question I once saw on a programming forum:
> Q: How can I fix the following warnings when I compile my program ...
> A: Add the directive (*$WARNINGS OFF*) to the top of your program.
> Just spend a week or so trying to maintain an application with 100,000's of lines of Delphi code if you really want to understand the horrendous problems associated with code that has multiple exits from thousands procedures.
OK, you and Jörg convinced me that the single RETURN function is a 'good thing', and that a compiler warning for multiple RETURNs would be an invitation to lax coding.
More information about the Oberon