[TUHS] I can't drive 55: "GOTO considered harmful" 55th anniversary

Dan Cross crossd at gmail.com
Sat Mar 11 05:04:15 AEST 2023


On Fri, Mar 10, 2023 at 1:55 PM Ron Natalie <ron at ronnatalie.com> wrote:
> That’s my point.  Both break structure.
>
> Just hinding the break with some construct that isn’t “goto” doesn’t
> make it acceptable.

The alternative is often worse, though. I'd take a well-aimed goto or
a nested break over a bunch of conditionals in the bodies of loops any
day, both in terms of readability and performance.

That said, I'm not a huge fan of the `goto fail;` pattern. Apple had a
pretty major bug with that, and I find that many, many times it can be
replaced with a restructuring of the code. For example, perhaps do
your allocation, call a function and capture the return value (passing
the allocated resources as arguments), and then de-allocate and return
the cached return value. Let the compiler optimize it.

As an experiment, I rewrote the Apple bug code (which was open source)
to do that and it was cleaner and simpler than the original. Someone I
showed it to said, "but what, are you going to do that for _every_
function that can fail?" Well no, but generally you don't have to; we
fixate too much on the general problem and ignore the specifics.

        - Dan C.


More information about the TUHS mailing list