Re: What do you think about the code?




Ian Collins wrote:
goose wrote:
Ian Collins wrote:

Frederick Gotham wrote:


<snipped>

In my own humble opinion, I think it's childish to outright ban something
when it comes to computer programming -- I feel it gives an air of
inconfidence in one's competence.


Well that's team democracy at work. We preferred well structured code
that was easy to refactor. Try extracting your inner loop to a function.

Do you program in a language other than C? Do you use exceptions
in those languages? Exceptions are just another form of gotos,
only its "goto that location and tell the code there that this
happened" and not just "goto the error location, it will
unconditionally execute code to handle errors". No one ever seemed
to have trouble refactoring code which generated excetpions.

I do, several. As you say in the next paragraph, exceptions provide a
structured way of passing control. They are /not/ another form of goto,
they do a lot more than simply jumping to another location. There are

Well, the issue going back and forth here is not "spurious usage
of gotos" is it? All the examples posted here were reasoned
and informed uses of gotos to *specifically* handle errors. I'd
hardly call the usage of gotos as we've seen here in this thread
/unstructured/. All were written to simulate exceptions in a language
which doesn't have built in support for exceptions.

Exceptions are a structured method for reporting errors; can you
disagree with that statement?
Using gotos to simulate exceptions would be as close as one
can get to exceptions in C.

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.

<snipped>

Don't confuse exceptions with gotos, the mechanisms are completely
different. A goto jumps to fixed location, if there are locks held, or
memory to be freed, tough.

How is this different from exceptions? I might be way off here, but I
always
assumed that exceptions could only unwind the stack, not magically
unlock or call free (or delete).

Anything allocated on the stack will get freed when longjmp gets
called, too.

Exceptions unwind the callstack, allowing
objects created on the stack to be destroyed and any resources they have
claimed to be freed.

The language (C) does not have support for allowing objects to
specify how they may be safely disposed off; I can envision one or
two ways that longjmp can be used to implement exceptions if the
language did indeed support destructors.

The only difference that I see between exceptions and longjmp is
that exceptions rely on a language feature not found in C.


Did you ban setjmp/longjmp too?

Yes. Remember this wasn't me, it was my team. I didn't tell them not
to use goto, they decided.

Sorry, I meant the plural "you" as in "You all".


[1]Well, in a conforming implementation. My current target
has a wierd paging/banking scheme that reuses addresses
so some functions have to load the registers with specific
values to switch banks, else the stack pointer might be
pointing to the stack of the main page. Needless to say,
set/longjmp won't work here and neither will exceptions
(C++ is supported) as they unwind stack but the jmp_buf
(and equivalent in an exception) although holding the value
it was initially set to, will be holding a value thats not valid
in the current context. For pretty much the same reason,
constructors and destructors don't always work the way
one would expect either.
Not that I care much other than in a vaguely academic
way; I write C for these targets :-)

goose,

.



Relevant Pages

  • Re: What do you think about the code?
    ... Well that's team democracy at work. ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Within a fixed scope, but you still identify the error handler (the ...
    (comp.lang.c)
  • Re: Java needs "goto" (was Re: hi)
    ... Assemblers need "goto" but no decent HLL does. ... I don't use this construction in C either and, again, don't need it. ... Yes - but that's a result of classes that throw exceptions when their methods would be better off returning control values. ... martin@ | Martin Gregorie ...
    (comp.lang.java.programmer)
  • Re: What do you think about the code?
    ... Within a fixed scope, but you still identify the error handler (the ... If exceptions are available, I'd rather throw ... I'd chuck in a goto rather than have the logic code do ... longjmp cannot call destructors automatically like ...
    (comp.lang.c)
  • Re: performance
    ... efficient language and still have an inefficient system. ... > Even on these super performant silicons, some kernel code and device drivers ... > exceptions. ... > relates to the efficiency/performance of the libraries ...
    (microsoft.public.dotnet.languages.csharp)