Board index » delphi » To goto or not to goto? Programming style question

To goto or not to goto? Programming style question

I confess: I'm a self-taught TP 5.0 programmer who is in the habit of using
gotos.  Now that I'm moving up to Delphi 2.01, which has various new
features, I'm wondering if there's a better way to do what I usually do.

Here is one sort of situation which comes up for me all the time:

for i:=1 to 1000 do begin
   [ do something]
   if [some condition holds] then goto line1;
   [do something else]
end;
[ do something else if you made it through the whole loop]
line1:
[ continue the program ]

One solution is to put everything before "line1" in a procedure, and use
exit instead of the goto.  But the price there is the time it takes to do
the procedure call.  Another option of course is to define some variable
holding a flag and setting the flag to true or false depending on
whether the [do something else if you made it through the whole loop ]
code is to be executed or not (then one can replace the goto by break).
But again, this takes a bit of extra time.

Any other options?  What is recommended procedure in this case?
(Sorry if this is a FAQ.)

David Hillman

 

Re:To goto or not to goto? Programming style question


Quote
> I confess: I'm a self-taught TP 5.0 programmer who is in the habit of > using gotos.  

   Bless you, my son, for you have sinned...

Quote
> Now that I'm moving up to Delphi 2.01, which has various new
> features, I'm wondering if there's a better way to do what I usually > do.
> Here is one sort of situation which comes up for me all the time:

> for i:=1 to 1000 do begin
>    [ do something]
>    if [some condition holds] then goto line1;
>    [do something else]
> end;
> [ do something else if you made it through the whole loop]
> line1:
> [ continue the program ]

  repeat
    { do something }
    if { not some condition holds } then
      { do something else }
  until { not some condition holds }

Quote
> One solution is to put everything before "line1" in a procedure, and use
> exit instead of the goto.  But the price there is the time it takes to do
> the procedure call.  

   See above.  It works, it's structured, and it's self-documenting...

Quote
> Another option of course is to define some variable
> holding a flag and setting the flag to true or false depending on
> whether the [do something else if you made it through the whole loop ]
> code is to be executed or not (then one can replace the goto by > break).
> But again, this takes a bit of extra time.
> Any other options?  

   Using a flag or other control variable is okay, too.  The main point
is that iteration doesn't _have_ to be done only with a For loop - the
Repeat and While are available, and they are designed for situations
where you don't execute the loop a finite number of times.  The While
permits you to avoid the loop altogether, if needed, and the Repeat
assures the loop will be executed at least once.  It's your choice for
the better statement.
   Why are you so concerned about "time"?  Is this truly a
performance-critical application, or what?  Such things shouldn't be
considerations for most work, and the effort/risk associated with
debugging unstructured code is much greater than the few milliseconds
you _might_ save in execution of your program.  As the adage says,
"Penny wise, pound foolish"...

Re:To goto or not to goto? Programming style question


On 18 Jan 1997 03:21:43 GMT,  dhillm...@pitt.edu (David  Hillman) wrote:

Quote
>I confess: I'm a self-taught TP 5.0 programmer who is in the habit of using
>gotos.  Now that I'm moving up to Delphi 2.01, which has various new
>features, I'm wondering if there's a better way to do what I usually do.

>Here is one sort of situation which comes up for me all the time:

>for i:=1 to 1000 do begin
>   [ do something]
>   if [some condition holds] then goto line1;
>   [do something else]
>end;
>[ do something else if you made it through the whole loop]
>line1:
>[ continue the program ]

It might not be a perfect match for your situation but TP 7.0 introduced
two new statements, continue and break.  These are akin to "C"
counterparts.  They can only be used inside the scope of a for, while,
or repeat statement.  

Break is a specialized "goto" that causes the innermost enclosing for,
while, or repeat statement to be exited immediately.  

Continue is a specialized "goto" that causes the innermost enclosing
for, while, or repeat statement to immediately proceed with the next
iteration.

I won't chastise you for using goto's, but statements that transfer
control abruptly, like break, continue, exit, and goto, should be used
sparingly.  The difference between your example above and your
suggestion to use a procedure with an early exit, is that with a
procedure, control moves smoothly through the main line.

I wouldn't be too concerned with the overhead of a near call and return.
The pair took 35 tics on an 8086 and only take 8 tics on a 486.  So not
only are machines getting faster, but the more common instructions are
getting more efficient.  

I would be more concerned with maintenance.  When you come back in 5 or
10 years are you going to be able understand and follow the code well
enough to install that new feature?

    ...red

Re:To goto or not to goto? Programming style question


David  Hillman <dhillm...@pitt.edu> wrote in article
<5bpfk7...@usenet.srv.cis.pitt.edu>...

Quote
> I confess: I'm a self-taught TP 5.0 programmer who is in the habit of using
> gotos.  Now that I'm moving up to Delphi 2.01, which has various new
> features, I'm wondering if there's a better way to do what I usually do.

Sure - it's improved!

Quote
> Here is one sort of situation which comes up for me all the time:
> for i:=1 to 1000 do begin
>    [ do something]
>    if [some condition holds] then goto line1;
>    [do something else]
> end;
> [ do something else if you made it through the whole loop]
> line1:
> [ continue the program ]
> One solution is to put everything before "line1" in a procedure, and use
> exit instead of the goto.  But the price there is the time it takes to do
> the procedure call.  Another option of course is to define some variable
> holding a flag and setting the flag to true or false depending on
> whether the [do something else if you made it through the whole loop ]
> code is to be executed or not (then one can replace the goto by break).
> But again, this takes a bit of extra time.

You could also use Break: it forces a break out of the loop. You still need a
flag, unless the Break is issued at the end of the loop - then you can test on
the value of i (well, you always can, but if it's 1000, you do know if [do
something else] is executed.

Quote
> Any other options?  What is recommended procedure in this case?

Since Delphi came, you have Exceptions. Here is an example:

try
  for i:=1 to 1000 do begin
     [ do something]
     if [some condition holds] then
       raise { your exception type }
     [do something else]
  end;
  [ do something else if you made it through the whole loop]
finally
  [ continue the program ]
end;

Bye,

--
  Zweitze de Vries
  Cyco Software
  Rijswijk ZH, Netherlands

Re:To goto or not to goto? Programming style question


Quote
David Hillman wrote:

> I confess: I'm a self-taught TP 5.0 programmer who is in the habit of using
> gotos.  Now that I'm moving up to Delphi 2.01, which has various new
> features, I'm wondering if there's a better way to do what I usually do.

> Here is one sort of situation which comes up for me all the time:

> for i:=1 to 1000 do begin
>    [ do something]
>    if [some condition holds] then goto line1;
>    [do something else]
> end;
> [ do something else if you made it through the whole loop]
> line1:
> [ continue the program ]

> One solution is to put everything before "line1" in a procedure, and use
> exit instead of the goto.  But the price there is the time it takes to do
> the procedure call.  Another option of course is to define some variable
> holding a flag and setting the flag to true or false depending on
> whether the [do something else if you made it through the whole loop ]
> code is to be executed or not (then one can replace the goto by break).
> But again, this takes a bit of extra time.

> Any other options?  What is recommended procedure in this case?
> (Sorry if this is a FAQ.)

> David Hillman

If you're still using Turbo Pascal, I'm afraid you're stuck.  But in a
book I read, there's nothing wrong if you're using a goto to get OUT of
something, just DON'T use it to get inside of something.

However, if you're moving up to Delphi, Borland now provided a "break"
statement for this purpose.  Hope this helps.

Re:To goto or not to goto? Programming style question


dhillm...@pitt.edu (David  Hillman) wrote:

Quote
>I confess: I'm a self-taught TP 5.0 programmer who is in the habit of using
>gotos.  Now that I'm moving up to Delphi 2.01, which has various new
>features, I'm wondering if there's a better way to do what I usually do.

   Many programming moving to pascal (from basic?!) usually tends to
use GOTO statement because it is supported.
  The rule of thumb is to avoid them whereever possible.  But my 5+
year experience tell me it can be totally eliminated. (I never use a
goto in any of my pascal statement)
   It may seems that using goto is direct  & fast.  But if you're
using lots of goto, you can easily come up with spaghetti code which
is hard to trace even the programmer who wrote them but put aside for
a while.  And when you're modifying old code, it will be much easier
as there won't be label right between what you're going to delete or
move or whatever.
  I don't know if you can goto right between loop. if that's the case,
compiler can't help in the control loop.

   { ... statement .... }
   goto  point1
   For I:=1 to 10 do
       begin
         statement 1;
  label point1      
         statement 2; {etc.}
       end;

I don;t know  if the compiler will generate error, it will be a mess
if you can do it this way!

       /=*         |
      /  |  __   __|  __    _  __  .............................
     /---| |  | |  | |__| |/  |__  .................Best Regards
    /    | |  | |__| |__  |    __| ......... and...@hk.super.net

Other Threads