lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Sep 25, 2005 at 10:14:05AM +0200, Philippe Lhoste wrote:
> destroy(c);
> errorC:
> destroy(b);
> errorB:
> destroy(a);
> errorA:

This type of cleanup gives me headaches.  I might occasionally point out
that "goto isn't *always* evil", but this is a textbook example of when
it is.  :)

A label and a distinct goto for every error check; they have to be in a very
specific order; getting the order wrong will lead to breakage or memory leaks
(and possibly in the worst possible place: an error path, which can make
them obscure); it doesn't handle very well the case where the objects are
created in a less trivial order (eg. put the "b" and "c" cases inside opposite
conditionals) ...

> When possible, I avoid mixing true boolean with NULL/0 values, which you 
> do in your cleanup code.

I try to, but it's valid and straightforward, so I often don't bother
(especially in example code typed into mail :).

> My code avoids tests, which is interesting only in time critical 
> applications, I suppose.

Error cleanup paths are very rarely time-critical, though.  I just don't
think that this style has any advantages (at least for the cleanup cases
we've looked at so far).

> IMHO, both codes are OK, your may be better suited to complex destroy cases.

My code is more straightforward for simple cases, too, IMO.

Glenn Maynard