Loop iterator - example.txt

Derek Parnell derek at psych.ward
Sun Apr 30 20:04:35 PDT 2006


On Mon, 01 May 2006 00:21:29 +0200, Alexander Panek wrote:


...

> A friend of mine once wrote such code:
> 
> <code>
> label:
> doSomething( );
> 
> if ( condition )
> 	goto label;
> </code>
> 
> That *is* a do-while loop.
> 
> <code>
> do {
> 	doSomething( );
> } while ( condition )
> </code>

The problem, IMHO, with this is not the 'goto' per se, but the label. 

The existence of a label opens the possibility that somewhere *else* in the
code is a reference to that label and so as a code maintainer, I must
examine all the code for such a reference. This takes time (even though it
might only be a seconds using a good editor) it does add further
maintenance cost. And if I do find other references, I need to examine the
implications of any change I make to the local area around the label to
ensure there are no unintended side-effects due to code jumping to the
label ... more time used up!

It is for that reason that controlled gotos such as 'do-while' are
preferred to uncontrolled gotos. The 'goto' statement is not the real
problem, its the label that's the issue.

In fact, I'm a firm believer that if a section of code contains a
'goto'/'label' in order to improve the efficiency or legibility then it is
a sign that there is a higher level construct missing in the language.
Knuth has given us a few examples where 'goto' does improve the reader's
knowledge of the intent of the author or removes redundantly executed code.
I regard this as a failing in the language used to encode the algorithms,
because the use of labels that can be reached from outside the context of
the immediate section is a maintenance cost we don't need.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
1/05/2006 12:46:16 PM



More information about the Digitalmars-d mailing list