Re: What do you think about the code?
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Tue, 04 Jul 2006 18:22:44 +1200
goose wrote:
Ian Collins wrote:Well I certainly couldn't justify a goto in a ten line function, then
goose wrote:
Ian Collins wrote:
Using gotos to simulate exceptions would be as close as one
can get to exceptions in C.
Within a fixed scope, but you still identify the error handler (the
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.
again I seldom write functions much longer than 10 lines :)
Exceptions.
I think you countered your own argument when you suggested exceptions
can be implemented with longjump, which is closer than goto.
Closer to what?
It's all a matter of taste and style. As my teams and I use Test Driven
Back to gotos: I still think that they have a use; sure theyplenty 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.
True, but we were talking about goto.
can make code unstructured, but then again just about any
construct can be abused to make code unstructured including
while, for, do and if.
Development, code is refactored often, so any construct that causes
problems with this isn't appropriate.
I've never actually *used* goto in any of my code (well, maybeAh, the truth emerges!
once :-) but as a maintainer I see "goto error_exit" all the
time in code that is reasonably easy to read.
--
Ian Collins.
.
- 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?
- From: goose
- Re: What do you think about the code?
- Prev by Date: Re: commercial c compilers vs free c compilers
- 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
|