[Info-vax] BASIC (and Horizon)
Arne Vajhøj
arne at vajhoej.dk
Thu Feb 1 21:49:56 EST 2024
On 1/31/2024 9:49 PM, Lawrence D'Oliveiro wrote:
> On Wed, 31 Jan 2024 19:29:14 -0500, Arne Vajhøj wrote:
>> That code would look a lot cleaner if the do while(false) loop got
>> removed and the relevant breaks got replaced by goto's.
>
> Show us how you would do it. I can take you through my code step by step,
> block by block, if that will help.
It is pretty simple.
do {
allocate(o1);
...
if(...) break;
...
} while(false);
deallocate(o1);
can be done as:
allocate(o1);
...
if(...) goto cleanup;
...
cleanup:
deallocate(o1);
The first piece of code is pretty bad as it confuses the
reader by using a loop and the break is not very
self documenting.
The second piece of code is straight forward to read
despite using goto.
It gets even more clear in the nested case.
do {
allocate(o1);
...
if(...) break;
...
flag = FALSE;
do {
allocate(o2)
...
if(...) {
flag = TRUE;
break;
}
...
}
deallocate(o2)
if(flag) break;
...
} while(false);
deallocate(o1);
vs:
allocate(o1);
...
if(...) goto cleanup_1;
...
allocate(o2);
...
if(...) goto cleanup_1_and_2;
...
cleanup_1_and_2:
deallocate(o2);
cleanup_1:
deallocate(o1);
The nested do while loop is a big mess. The goto solution
is still simple.
Arne
More information about the Info-vax
mailing list