Date: Tue, 20 Jul 2010 16:02:11 -0400
Reply-To: Ian Whitlock <iw1sas@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1sas@GMAIL.COM>
Subject: Is LEAVE "non-structured"?
Content-Type: text/plain; charset=ISO-8859-1
Data _null_ and Robert,
The argument is an old one from the 1960's.
First let's separate languages. You cannot argue that I can use GOTO
in SAS because the computer at some point executes a jump or goto.
That would be similar to saying SAS should be written as strings of
0 and 1 because that is what is ultimately implemented. The whole
point of a higher level language is to be free of the method of
implementation. So the question becomes should a high level language
have an explicit GOTO.
The disadvantages:
1) Code quickly becomes unreadable with arbitrary GOTOs.
2) Proving code correct becomes much harder with explicit GOTOs.
The advantages:
A well placed GOTO can make the code more readable. Example, escaping
from the middle of a nested sequence of loops.
The vote of history appears to be that most languages have an explicit
GOTO and some of those that did not start with one soon added a GOTO.
The biggest problem with unreadable GOTOs is their uncontrolled nature.
If one only allows transfer to a later point in the code the unreadable
problem largely disappears. LEAVE enforces that policy.
So what is the difference between blah, blah, goto and blah, blah, leave.
In the second case the reader immediately knows what has happened. In the
first case the reader must find the target location to confirm what the
reader of the second case already knew. If you are the only reader of
your code and you follow a good policy in the use of GOTOs then it
doesn't matter much.
Personally I would not write GOTO when LEAVE accomplishes the same thing.
However, it does not bother me much to write
%goto mexit ;
in macros although the twinges have gotten stronger since %RETURN has
been introduced.
In short there are good reasons to avoid GOTO's and good reasons to
allow one once in while. Hence, in the current state of the art of
writing computer languages, the language should not prevent a GOTO, but
the programmer should most of the time not use them.
Now what about LEAVE versus WHILE and UNTIL? WHILE guarantees exit at
the top of the loop and UNTIL guarantees it at the bottom. Code is
somewhat easier to read if you know exactly where the exit is and under
what conditions it will happen. On the other hand, a simple LEAVE in
the middle of a loop may be clearer than setting a flag and using an
IF/ELSE construction to make the code exit at the bottom of a loop.
A second LEAVE makes even more true.
When one starts having loops involving many lines and many LEAVEs then
it is time to leave the code and work on a better design.
Rules are aids to writing readable programs, but the programmer should
understand why the rule, so that he/she can know when it is wise to
break the rule.
Ian Whitlock
===============
Date: Mon, 19 Jul 2010 09:09:32 -0500
From: "Data _null_;" <iebupdte@GMAIL.COM>
Subject: Is LEAVE "non-structured"?
Content-Type: text/plain; charset=ISO-8859-1
In a reply from Robert Rosofsky in the "Combining iterative DO and
UNTIL condition" where I suggested the LEAVE statement he indicated
[quote]The use of the LEAVE command feels kind of "non-structured,"
but I suppose I can live with that.[/quote]
I use LEAVE often and sometimes prefer it to WHILE/UNTIL. I used GOTO
before we had LEAVE and still use GOTO when "I think I need to".
Aren't all the programming constructs are just dressed up GOTOs.
So is LEAVE non-structured?