Re: Simple question!
"Michael K. O'Neill" <MikeAThon2000@nospam.hotmail.com> skrev i
meddelandet news:%23eTnW5TpGHA.4188@TK2MSFTNGP04.phx.gbl...
See in-line below....
"Bo Persson" <bop@gmb.dk> wrote in message
news:4hhd1eF1rqoqtU1@individual.net...
"Alex Blekhman" <xfkt@oohay.moc> skrev i meddelandet
news:OFi00NLpGHA.516@TK2MSFTNGP05.phx.gbl...
I can see that many attribute some inherently evil properties to
`goto' statement. Michael K. O'Neill already posted good article
in
this thread. I'd second his opinion. There is nothing wrong with
`goto' per se. All of us have seen horrible code without single
`goto'. This is not that `goto' (or any other language construct)
that makes code bad; it's quite the opposite: bad, stupid coders
produce filthy code (no matter what the language the use for
that).
Any feature in a programming language is just a tool. Use it
wisely
and you'll get beautiful elegant code. I just can't get all these
rumbles about `goto'.
Because it is an inherently unstructured way to get from one
arbitary
point in the program to another? :-)
When 99.9% of the uses are bad, the general advise to students is:
"Don't use it"!
Frank Rubin is simply wrong! Even though he has found the break
statement, he doesn't see how to use it to avoid the goto.
His Pascal code:
for i := 1 to n
do begin
for j :=1 to n do
if x[i, j] <> 0 then
goto reject;
writeln('The first all-zero row is ', i);
break;
reject: end;
could easily have been written (C++ style, 0 based array):
bool is_all_zero_row(int row)
{
for (int col = 0; col != n; ++col)
if (x[row][col] != 0)
return false;
return true;
}
// ...
for (int i = 0; i < n; ++i)
{
if (is_all_zero_row(i))
{
std::cout << "The first all-zero row is " << i << std::endl;
break;
}
}
See Ma, no gotos !
One of the central premises of Dijkstra's structured programming
concepts
was that each block of code should have exactly one entry point and
exactly
one exit point. The reason for this requirement related to flow
control and
proofs of correctness.
Ok.
The "ban" on goto's is an offshoot of this "one entry/one exit"
requirement.
Your example, which uses multiple "returns" to exit from multiple
different
places in the is_all_zero_row() function, manifestly violates this
"one
entry/one exit" requirement of structured programming.
No, it's not. It is just a short-curcuit version of:
bool is_all_zero_row(int row)
{
bool result = true;
for (int col = 0; col != n; ++col)
if (x[row][col] != 0)
result = false;
return result;
}
My original version just returns as soon as it has determined what the
result will be. There isn't much correctness to win by continuing,
when you already know the final result. Unlike a goto, a return
statement has a known target - the point of call.
It's simply not
properly structured from a "structured programming" perspective. In
fact,
if you think about it, multiple "returns" is the full moral
equivalent of
goto statements, and should be avoided for exactly the same reasons.
No, it is not. A goto statement is totally unstructured, in that its
result depends on both the goto statement, and the placement of the
label.
Compare
bool is_all_zero_row(int row)
{
bool result = true;
for (int col = 0; col != n; ++col)
if (x[row][col] != 0)
{
result = false;
goto end;
}
end:
return result;
}
and
bool is_all_zero_row(int row)
{
bool result = true;
end: //oops
for (int col = 0; col != n; ++col)
if (x[row][col] != 0)
{
result = false;
goto end;
}
return result;
}
That can never happen with a return statement!
Bo Persson