On Tue, Feb 17, 2004 at 07:21:00PM +0000, Will Newton wrote:
> On Tuesday 17 Feb 2004 4:58 pm, Andrew Suffield wrote:
>
> > > void shoe()
> > > {
> > > /* Do some complicated stuff */
> > > if (foo == NULL)
> > > goto cleanup;
> > > /* Continue complicated stuff */
> > > if (bar == 3)
> > > goto cleanup;
> > >
> > > cleanup:
> > > /* Cleanup */
> > > }
> >
> > Good grief, that's *terrible* C. Don't do things like that. The
> > problem here is not a lack of exceptions, it's just bad code. As
> > usual.
>
> As usual? OK.
>
> That is an often used convention, used by many substantial pieces of C code
> that you are running on your system right now. Admittedly it is much more
> often used in system level situations than application code. It can be argued
> that much C is *terrible*. That's why it's C.
Your point is that there is a lot of bad C code around? Yes, that's
certainly true, but it's not an excuse for writing more bad C.
> > Here's how you should do that:
>
> <snip>
>
> > static bool
> > process_foo(struct foo *foo)
> > {
> > if (!check_foo(foo))
> > {
> > /* report and die */
> > }
> >
> > /* process it */
> > }
> >
> > (Most uses of goto should be replaced with function calls; the rest
> > should be replaced with block-structure statements)
>
> Unfortunately this doesn't necessarily work if you need to do work
> (allocations, side effects) in between checking foo and bar.
It does if you do it right. I don't exactly write this sort of code
infrequently; it's quite easy to structure your code in a manner that
works neatly and effectively.
--
.''`. ** Debian GNU/Linux ** | Andrew Suffield
: :' : http://www.debian.org/ |
`. `' |
`- -><- |
Attachment:
signature.asc
Description: Digital signature