Re: What do you think about the code?
- From: "goose" <ruse@xxxxxxxxxxxxx>
- Date: 3 Jul 2006 22:33:56 -0700
Ian Collins wrote:
goose wrote:
Ian Collins wrote:
<snipped>
Exceptions are a structured method for reporting errors; can you
disagree with that statement?
I haven't, if you read what I posted, you'll see I agreed.
Just making sure :-)
Using gotos to simulate exceptions would be as close as oneWithin a fixed scope, but you still identify the error handler (the
can get to exceptions in C.
label) at the point of error, where throwing an exception does not.
Thats right; However I remain to be convinced that this is such
a bad thing. For a function of, say, ten lines (with the error-handling
code) I'd not hesitate to use a goto *when the error handler is
in the same scope*. If exceptions are available, I'd rather throw
an exception but if exceptions are *not* there for me to use
I'd chuck in a goto rather than have the logic code do
unneeded checks.
I think you countered your own argument when you suggested exceptions
can be implemented with longjump, which is closer than goto.
Closer to what?
True, but we were talking about goto.
plenty of techniques for writing exception safe code. I can't think of
any for writing goto safe code!
If exceptions simply did a longjump, people wouldn't use them.
Fair enough; longjmp cannot call destructors automatically like
C++/Java/etc exceptions can but it *can* unwind the stack.
The jmp_buf initialised with setjmp only gets invalidated when the
function scope where setjmp is called returns[1]. I'd assume that,
with language support for destructors (variables going out of
scope automatically get a certain function called), exceptions
can be implemented with longjmp.
Fair enough; I sorta dragged longjmp into this argument because
I consider it to be fairly close in function to a goto. We can
always drop the longjmp argument if you feel that it's a little
more structured.
Back to gotos: I still think that they have a use; sure they
can make code unstructured, but then again just about any
construct can be abused to make code unstructured including
while, for, do and if.
I've never actually *used* goto in any of my code (well, maybe
once :-) but as a maintainer I see "goto error_exit" all the
time in code that is reasonably easy to read.
OTOH, I've had occasion to completely rewrite some "code"
(and I use that term loosely!) because it was so badly written
that it was faster to rewrite than try to figure out how it
worked and what all the status variables meant; yet the code
contained no gotos and only the usual suspects (while, for, etc).
Theres probably a lesson in there somewhere. Perhaps the
"goto error_exit" users are just more likely to have
structured their functions into "init, solve, exit" than
the coders who avoid goto at all costs.
goose,
.
- Follow-Ups:
- Re: What do you think about the code?
- From: Ian Collins
- 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?
- From: Andrew Poelstra
- Re: What do you think about the code?
- From: Ian Collins
- Re: What do you think about the code?
- From: Frederick Gotham
- Re: What do you think about the code?
- From: Ian Collins
- Re: What do you think about the code?
- From: goose
- Re: What do you think about the code?
- From: Ian Collins
- Re: What do you think about the code?
- From: goose
- Re: What do you think about the code?
- From: Ian Collins
- Re: What do you think about the code?
- Prev by Date: Re: how to initial and print the unicode character?
- Next by Date: Re: how to initial and print the unicode character?
- 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
|