comp.lang.ada
 help / color / mirror / Atom feed
* Re: about the exit; statment
  2000-03-22  0:00 about the exit; statment Tim Gahnström
@ 2000-03-22  0:00 ` Ted Dennison
  0 siblings, 0 replies; 2+ messages in thread
From: Ted Dennison @ 2000-03-22  0:00 UTC (permalink / raw)


In article
<Pine.SOL.4.21.0003221546140.12440-100000@fraggel84.mdstud.chalmers.se>,
=?ISO-8859-1?Q?Tim_Gahnstr=F6m?= <md9tim@mdstud.chalmers.se> wrote:
> About the goto thread
> Someone sugestet exit instead.
>
> I am studying right now and whe have been thought that in general exit
> statments arent verry good either.
>
> (Though the Teacher have never even mentioned that the goto statment
> exists!)

My respect for your teacher grows with each post you make. :-)


> Are these two statments a little equvivalent in terms of readability?

What we are really talking about here is how easy it is for someone else
(or yourself several months later) to figure out the control flow of
your program. Looking at it this way, you should soon be able to make
such judgments for yourself.

For instance, a "for" loop is nice because you know without having to
examine any other code exactly what code is being repeated and how many
times (assuming no "exit" or "return" or "goto"). A while loop is a
smidge harder to figure out, because the reader has to read the contents
of the loop to figure out exactly what will cause an exit.

A "loop" with a single "exit" is a bit worse than a "while" because now
the code that gets repeated isn't so clearly defined; some of it gets
repeated one time less than the rest. Of course sometimes this is
exactly what you need to have happen. In that case an "exit" is probably
the way to go. But it is tougher to understand that a "while" loop that
doesn't have this behavior at all.

A "loop" with multiple "exit"s or "returns" is worse still because there
are multiple different ways the code could get repeated that the poor
user has to figure them all out to understand what is going on.

A "goto" is worst of all because potentially *any* code could get
repeated. The user has to figure out for themselves if "goto" is used
for a loop, or to exit from one. If it is used for a loop, there is no
clear language-enforced limit to what code gets repeated, like the "end"
statement is for normal loops. If its used to exit a loop, it could
potientially carry the program's control anywhere, not just to the next
statement after the loop as most other loop exit methods will do.

Hopefully your teacher will be impressing upon you the importance of
readability. It should be a very close second to getting it your code to
work (and you will find they often go hand-in-hand).

So really what we have here is a hierarchy of methods that can be used
to direct program looping and control. In order of preference they are:

for loop
while loop
middle or end-tested loop
multiple-exited loop
goto

In any given situation you want to choose the one that is as high up the
list as possible. The big catch is that the more powerful methods are
lower on the list. If you actually need something more complex than a
simple "for" loop you will have to rearrage the structure of things a
bit to make it fit in a "for" loop. Of course there comes a point when
that mangles things so badly that one of the more powerful loops like a
"while" would actually be clearer. The principle applies all the way
down the list.

As a student you need to teach yourself where those lines are.
Eventually it will become intuition, but at first you will probably find
it useful to implement your loops in several different ways to see which
is clearer. Don't be afraid to do this. In fact, when multiple exit
loops are in question, I typically still do this.

In my personal work I find that I use the top loop ("for") wherever
possible. The top three constitute the majority of my loops. The second
to last I use in special circumstances (typically searches) with copious
comments. The last I have never used in 11 years in the industry.

--
T.E.D.
http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 2+ messages in thread

* about the exit; statment
@ 2000-03-22  0:00 Tim Gahnström
  2000-03-22  0:00 ` Ted Dennison
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Gahnström @ 2000-03-22  0:00 UTC (permalink / raw)


About the goto thread 
Someone sugestet exit instead.

I am studying right now and whe have been thought that in general exit
statments arent verry good either.

(Though the Teacher have never even mentioned that the goto statment
exists!)

Are these two statments a little equvivalent in terms of readability?

Tim





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-03-22  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-22  0:00 about the exit; statment Tim Gahnström
2000-03-22  0:00 ` Ted Dennison

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