Re: What do you think about the code?
- From: Andrew Poelstra <apoelstra@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 01 Jul 2006 17:18:49 GMT
On 2006-07-01, Richard Heathfield <invalid@xxxxxxxxxxxxxxx> wrote:
Andrew Poelstra said:
<snip>
What is easier to read:
/* Start */
while (test[0])
while (test[1])
while (test[2])
{
if (test[3])
goto outOfLoop;
}
outOfLoop:
puts ("Escaped from loop.");
Now add sixty to a hundred lines of actual code, ten lines of comments, and
a couple of years of maintenance, and /then/ ask me whether this is easy to
read.
Or, without goto:
Unstructured version snipped. The break keyword is just goto in a false
moustache.
In practice, status objects are cheap and readable. If they are becoming
unreadable, it's a sign that your function is too big. One huge advantage
of a modular design with strict avoidance of goto, non-documentary
continue, and switchless break, is that it is really easy to refactor a
function that has outgrown its readability. Just chop out the innermost
loop, dump it into a new function, and that's typically almost all that you
have to do. When the control flow is hopping all over the place like a frog
on his wedding night, code simplification is far more difficult to achieve.
Since as a maintenance programmer you've probably had to deal with a lot of
similar code, suppose you were writing a parser and you did this:
c = get_input();
if (we_are_in_a_quote && c != '\"')
goto skip_parsing;
switch (c)
{
/* Parsing code here */
}
skip_parsing:
/* Return parsed text to callee. */
Would that be an appropriate use of goto? Otherwise I've found that I end up
with if statements around most every single case block. (I can't do it around
the whole switch because I don't care if I'm in quotes when checking for the
quote character and the escape character).
--
Andrew Poelstra <http://www.wpsoftware.net/blog>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
.
- Follow-Ups:
- Re: What do you think about the code?
- From: Ian Collins
- Re: What do you think about the code?
- From: Richard Heathfield
- Re: What do you think about the code?
- References:
- Re: What do you think about the code?
- From: Richard Heathfield
- Re: What do you think about the code?
- Prev by Date: Re: What do you think about the code?
- Next by Date: Re: comp.lang.c FAQs - a note of thanks
- Previous by thread: Re: What do you think about the code?
- Next by thread: Re: What do you think about the code?
- Index(es):
Relevant Pages
|