[FFmpeg-devel] Use of GOTO's [WAS: [PATCH] RoQ encoder]

Gary Corcoran gcorcoran
Sun May 6 02:02:34 CEST 2007

Guillaume Poirier wrote:
> Hi,
> On May 5, 2007, at 10:35 , Michael Niedermayer wrote:
>> On Sat, May 05, 2007 at 10:08:25AM +0200, Guillaume Poirier wrote:
>>> On May 5, 2007, at 3:00 , Rich Felker wrote:
>>>> On Sat, May 05, 2007 at 12:57:35AM +0100, Robert Swain wrote:
>>>>> Out of personal interest and being a lesser experienced programmer,
>>>>> I've always been lead to believe that 'goto' shouldn't be used  
>>>>> unless
>>>>> absolutely necessary. Grepping the FFmpeg source throws up far more
>>>>> results than I expected so now I'm wondering - why have you  
>>>>> suggested
>>>>> the use of goto over something like a 'while' loop? If it's  
>>>>> better to
>>>>> use a goto in some cases, I'd like to know when and why. :)
>>>> This is nonsensical dogma. Continue and break statements are exactly
>>>> the same thing as goto. Use whatever control construct makes  
>>>> sense to
>>>> the code you're writing. Inserting dummy loops so you can break just
>>>> to "hide" your goto in a break statement is ridiculous. If you mean
>>>> goto, say goto.
>>>> FWIW, goto is extremely useful for error case handling

Yes - see my comments below.

>>>> can cleanup before error returns.
>>> It's maybe useful for the programmer, but it confuses compilers so
>>> that some optimization passes can't be performed on a code with  
>>> goto's.
>>> Well, maybe not all compilers are like this, but i bet it confuses
>>> GCC, who's not the smartest compiler around...
>> ffmpeg contains a few gotos, can you point to one where the code  
>> would be
>> faster without the goto?
> I'll try my best to gather this information for you. I was merely  
> repeating what one of our corporate in-house genius has told me to  
> explain why GOTO are evil. I'm now convinced that they should really  
> be avoided in all cases.

Are GOTO's evil?  No.  Should they be used indiscriminately?  No.  You
are most likely correct that some uses of GOTO's instead of 'for' or
'while' loops for simple situations will not allow the compiler to
understand enough to optimize the code.  When you don't have to bend
over backwards to use them, for/while/do loops are a better programming
practice.  But should they be avoided in *all* cases?  Certainly not.
As Michael mentioned, sometimes they can be used for programmer-optimized
(as opposed to compiler-optimized) code, to run faster.  And...

Most of you are probably too young to remember, but around 1990 the
entire AT&T U.S. phone network went out for several hours.  The cause
was eventually traced to one line of code, which I believe was trying
to handle an error condition.  The programmer said "break;".  But what
he forgot was that he was in a nested situation.  Like either a for
loop within a case statement, or within a switch within an outer for or
while loop.  And so instead of breaking all the way out of the code, it
only broke out of the inner part, and chaos ensued, eventually taking
out phone switch after phone switch, across the entire country, after
that error condition was hit for the first time (and happened to cause
a domino effect).

What the programmer should have done was use a 'goto' to get completely
out of the nested code, and go to the error cleanup part of the routine.
But he was probably taught that GOTOs should always be avoided...


More information about the ffmpeg-devel mailing list