<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using goto for error handling in C</title>
	<atom:link href="http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 28 Jul 2010 12:22:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ram Dobson</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-297559</link>
		<dc:creator>Ram Dobson</dc:creator>
		<pubDate>Mon, 19 Jul 2010 22:05:47 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-297559</guid>
		<description>and oh, i can see the point about not blindly avoiding goto, and if the multiple returns upset some people, you could do the same thing like this:


`&lt;code&gt;function foo {

var1 = allocate_stuff();
result = 0;
if ( !next_foo(bar)) {
  cleanup_1();
} else {
  result = do_the_right_thing(bar);
}
return result
}&lt;/code&gt;`

and i should admit that i am offering *3* tight functions to replace one messy one, which is a less clear advantage, but in my book, still quite preferable.</description>
		<content:encoded><![CDATA[<p>and oh, i can see the point about not blindly avoiding goto, and if the multiple returns upset some people, you could do the same thing like this:</p>
<div class="backtick"><pre><code>function foo {

var1 = allocate_stuff();
result = 0;
if ( !next_foo(bar)) {
  cleanup_1();
} else {
  result = do_the_right_thing(bar);
}
return result
}</code></pre></div>
<p>and i should admit that i am offering *3* tight functions to replace one messy one, which is a less clear advantage, but in my book, still quite preferable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ram Dobson</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-297557</link>
		<dc:creator>Ram Dobson</dc:creator>
		<pubDate>Mon, 19 Jul 2010 21:58:10 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-297557</guid>
		<description>I think i would still use ifs here, and if things got too deep, like in the second example i&#039;d do something like this

`function foo {

  var1 = allocate_stuff();
  if ( !next_foo(bar)) {
    cleanup_1();
    return 0;
  }
  return do_the_right_thing(bar);
}

and do the inner stuff in the next_foo function.

now i can already see that this doesn&#039;t pass all the state in to the second function,
but seriously... do you want to maintain the giant mess of code that is one function
where everything is passed implicitly from the parent scope? i didn&#039;t think so.

if next_foo depends on var1, then next_foo(bar,var1) it makes it clear that the remainder depends on var1. much nicer, and next_foo will be clear.

i&#039;m sorry, but your argument that the second version looks ok now, but if it was a giant mess, it would be messy, just doesn&#039;t ring true. If the function was a mess, then you&#039;ve got too much going on in one function... and breaking out goto isn&#039;t gonna make that turd any shinier.

In your version of the big messy function the creation and cleanup of thing1 are like miles apart, on opposite ends of a giant mess, connected by a goto... ick.

compare that with my version, a small tight function, which creates something, has  calls out to &quot;try_to_continue&quot; (next_foo) and cleans up if that fails.</description>
		<content:encoded><![CDATA[<p>I think i would still use ifs here, and if things got too deep, like in the second example i&#8217;d do something like this</p>
<p>`function foo {</p>
<p>  var1 = allocate_stuff();<br />
  if ( !next_foo(bar)) {<br />
    cleanup_1();<br />
    return 0;<br />
  }<br />
  return do_the_right_thing(bar);<br />
}</p>
<p>and do the inner stuff in the next_foo function.</p>
<p>now i can already see that this doesn&#8217;t pass all the state in to the second function,<br />
but seriously&#8230; do you want to maintain the giant mess of code that is one function<br />
where everything is passed implicitly from the parent scope? i didn&#8217;t think so.</p>
<p>if next_foo depends on var1, then next_foo(bar,var1) it makes it clear that the remainder depends on var1. much nicer, and next_foo will be clear.</p>
<p>i&#8217;m sorry, but your argument that the second version looks ok now, but if it was a giant mess, it would be messy, just doesn&#8217;t ring true. If the function was a mess, then you&#8217;ve got too much going on in one function&#8230; and breaking out goto isn&#8217;t gonna make that turd any shinier.</p>
<p>In your version of the big messy function the creation and cleanup of thing1 are like miles apart, on opposite ends of a giant mess, connected by a goto&#8230; ick.</p>
<p>compare that with my version, a small tight function, which creates something, has  calls out to &#8220;try_to_continue&#8221; (next_foo) and cleans up if that fails.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: emacs</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-289817</link>
		<dc:creator>emacs</dc:creator>
		<pubDate>Tue, 22 Jun 2010 12:00:43 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-289817</guid>
		<description>I often use 
do { 
      ....
 } while(0); 

error handling
 
instead of goto</description>
		<content:encoded><![CDATA[<p>I often use<br />
do {<br />
      &#8230;.<br />
 } while(0); </p>
<p>error handling</p>
<p>instead of goto</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: x</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-178110</link>
		<dc:creator>x</dc:creator>
		<pubDate>Tue, 23 Jun 2009 17:32:36 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-178110</guid>
		<description>I&#039;m way late to the game, but in the spirit of being a smart ass....


&quot;The classics are books everyone has heard of but noone has read&quot;

seems apt.



What do you think a while loop does, if not a &quot;hidden&quot; goto:


while( condition() ) {
  do_this();
}

/* versus: */

do_this_loop:
  if ( condition() ) {
    do_this();
    goto do_this_loop;
  }


If you&#039;re gonna bitch about gotos, isn&#039;t it hypocritical and completely &quot;head in the sand&quot;-ish to use while loops? Why are you using processors with JMP instructions? Please show me this new computer of yours that works differently.


Assuming you choose a good label name, don&#039;t gotos better promote &quot;self-documenting code&quot; ?



Jussi JumppanenNo Gravatar Says:
April 27th, 2009 at 08:26

In most cases code can be restructured to not require the uses of a goto  statement.

Jussi: Why? Why do *you* think goto is bad and that is necessary?
Because other people told you it is bad?



Pádraig BradyNo Gravatar Says:
April 27th, 2009 at 10:33

Note for the common case of a common cleanup, one can:

Pádraig: I fail to see how this is any clearer, and it is harder to decipher what is really going on. Again, no justification given to why one would want to do this.



qNo Gravatar Says:
April 29th, 2009 at 09:19

If your code is to be MISRA compliant, must pass a pretty severe QA-C test, and needs 100% test coverage on all decisions in your code, you better not use goto, or multiple returns (a.o.)


If whatever tool(s) you use can&#039;t determine when all the paths in your code are taken when you use gotos or multiple returns(), isn&#039;t that more a problem of the tool you are using, or maybe the fact you are using C when you should be using something else?
Why can&#039;t that tool &quot;parse&quot; or work with valid C code?

I realize limiting yourself to a subset of C is a very good thing in certain cases, especially in regards to &quot;proving&quot; things about code.

Are you saying you *would* use multiple returns and gotos on code that did not need to be ran against those particular tools / QA processes?

My understanding is the QA and test coverage is a job requirement, but that makes your argument less valid to me -- you are only against goto because your job won&#039;t let you use them.




I am in favor of multiple return statements. If you know the &quot;answer&quot; to a function, why dilly-dally, instead of return it immediately, as soon as you know?

Sure, you can use nested ifs, but that makes for hairy hard-to-follow code, and huge indention leads to things not fitting on a single screen (whether vertically or horizontally) of your editor.


Comments? I hope my self-righteous tone is enough for some posters here to realize theirs. Do some people really expect we should believe them because their name is attached? Or someone else said so? And nary a word of *why* they feel the way they do, and what brought them there?


Yeah, I like to debate about things that might not matter, but if you didn&#039;t want a debate, why did you post on this topic? Or why did you post on a topic you don&#039;t care about?


I am not the perfect person or coder by far. But I think a good person is thinking for themselves and not repeating what they are told, *especially* when it comes from the &quot;mouths of the gods&quot;, be it coding or whatever.

(yes, I will take some chill pills).

Sorry, I can only hope Djikstra had good intentions and would have been happy his message &quot;got out&quot; eventually. But I also hope he would have  utter shock and disbelief at how many people swallow it now w/out a second thought.</description>
		<content:encoded><![CDATA[<p>I&#8217;m way late to the game, but in the spirit of being a smart ass&#8230;.</p>
<p>&#8220;The classics are books everyone has heard of but noone has read&#8221;</p>
<p>seems apt.</p>
<p>What do you think a while loop does, if not a &#8220;hidden&#8221; goto:</p>
<p>while( condition() ) {<br />
  do_this();<br />
}</p>
<p>/* versus: */</p>
<p>do_this_loop:<br />
  if ( condition() ) {<br />
    do_this();<br />
    goto do_this_loop;<br />
  }</p>
<p>If you&#8217;re gonna bitch about gotos, isn&#8217;t it hypocritical and completely &#8220;head in the sand&#8221;-ish to use while loops? Why are you using processors with JMP instructions? Please show me this new computer of yours that works differently.</p>
<p>Assuming you choose a good label name, don&#8217;t gotos better promote &#8220;self-documenting code&#8221; ?</p>
<p>Jussi JumppanenNo Gravatar Says:<br />
April 27th, 2009 at 08:26</p>
<p>In most cases code can be restructured to not require the uses of a goto  statement.</p>
<p>Jussi: Why? Why do *you* think goto is bad and that is necessary?<br />
Because other people told you it is bad?</p>
<p>Pádraig BradyNo Gravatar Says:<br />
April 27th, 2009 at 10:33</p>
<p>Note for the common case of a common cleanup, one can:</p>
<p>Pádraig: I fail to see how this is any clearer, and it is harder to decipher what is really going on. Again, no justification given to why one would want to do this.</p>
<p>qNo Gravatar Says:<br />
April 29th, 2009 at 09:19</p>
<p>If your code is to be MISRA compliant, must pass a pretty severe QA-C test, and needs 100% test coverage on all decisions in your code, you better not use goto, or multiple returns (a.o.)</p>
<p>If whatever tool(s) you use can&#8217;t determine when all the paths in your code are taken when you use gotos or multiple returns(), isn&#8217;t that more a problem of the tool you are using, or maybe the fact you are using C when you should be using something else?<br />
Why can&#8217;t that tool &#8220;parse&#8221; or work with valid C code?</p>
<p>I realize limiting yourself to a subset of C is a very good thing in certain cases, especially in regards to &#8220;proving&#8221; things about code.</p>
<p>Are you saying you *would* use multiple returns and gotos on code that did not need to be ran against those particular tools / QA processes?</p>
<p>My understanding is the QA and test coverage is a job requirement, but that makes your argument less valid to me &#8212; you are only against goto because your job won&#8217;t let you use them.</p>
<p>I am in favor of multiple return statements. If you know the &#8220;answer&#8221; to a function, why dilly-dally, instead of return it immediately, as soon as you know?</p>
<p>Sure, you can use nested ifs, but that makes for hairy hard-to-follow code, and huge indention leads to things not fitting on a single screen (whether vertically or horizontally) of your editor.</p>
<p>Comments? I hope my self-righteous tone is enough for some posters here to realize theirs. Do some people really expect we should believe them because their name is attached? Or someone else said so? And nary a word of *why* they feel the way they do, and what brought them there?</p>
<p>Yeah, I like to debate about things that might not matter, but if you didn&#8217;t want a debate, why did you post on this topic? Or why did you post on a topic you don&#8217;t care about?</p>
<p>I am not the perfect person or coder by far. But I think a good person is thinking for themselves and not repeating what they are told, *especially* when it comes from the &#8220;mouths of the gods&#8221;, be it coding or whatever.</p>
<p>(yes, I will take some chill pills).</p>
<p>Sorry, I can only hope Djikstra had good intentions and would have been happy his message &#8220;got out&#8221; eventually. But I also hope he would have  utter shock and disbelief at how many people swallow it now w/out a second thought.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kif</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-169123</link>
		<dc:creator>kif</dc:creator>
		<pubDate>Wed, 29 Apr 2009 19:25:52 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-169123</guid>
		<description>@g, I&#039;d never heard of MISRA but upon reading their mission statement:

&quot;To provide assistance to the automotive industry in the application and creation within vehicle systems of safe and reliable software.&quot;

I&#039;m pretty damn glad they exist ;).</description>
		<content:encoded><![CDATA[<p>@g, I&#8217;d never heard of MISRA but upon reading their mission statement:</p>
<p>&#8220;To provide assistance to the automotive industry in the application and creation within vehicle systems of safe and reliable software.&#8221;</p>
<p>I&#8217;m pretty damn glad they exist <img src='http://eli.thegreenplace.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: q</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-169015</link>
		<dc:creator>q</dc:creator>
		<pubDate>Wed, 29 Apr 2009 07:23:45 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-169015</guid>
		<description>Now I come to read Twylite&#039;s comment,
yes, a break inside some loop is a third way to ensure the ordering of new fresh peck and a set of feathers.</description>
		<content:encoded><![CDATA[<p>Now I come to read Twylite&#8217;s comment,<br />
yes, a break inside some loop is a third way to ensure the ordering of new fresh peck and a set of feathers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: q</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-169013</link>
		<dc:creator>q</dc:creator>
		<pubDate>Wed, 29 Apr 2009 07:19:27 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-169013</guid>
		<description>@elibenSays
Multiple returns are made by the devil too. Using it yields an equal result: you will be put against the wall as well.
Well actually, you&#039;re right, we stopped shooting people a while ago. We use peck and feathers now.

If your code is to be MISRA compliant, must pass a pretty severe QA-C test, and needs 100% test coverage on all decisions in your code, you better not use goto, or multiple returns (a.o.)

About that &quot;judiciously using a construct&quot;, I understand you. But do also know that consistency has to be taken in account.

For Christ&#039;s sake, what am I arguing here? What will be defended next? Basica rules?</description>
		<content:encoded><![CDATA[<p>@elibenSays<br />
Multiple returns are made by the devil too. Using it yields an equal result: you will be put against the wall as well.<br />
Well actually, you&#8217;re right, we stopped shooting people a while ago. We use peck and feathers now.</p>
<p>If your code is to be MISRA compliant, must pass a pretty severe QA-C test, and needs 100% test coverage on all decisions in your code, you better not use goto, or multiple returns (a.o.)</p>
<p>About that &#8220;judiciously using a construct&#8221;, I understand you. But do also know that consistency has to be taken in account.</p>
<p>For Christ&#8217;s sake, what am I arguing here? What will be defended next? Basica rules?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Star bright</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-168895</link>
		<dc:creator>Star bright</dc:creator>
		<pubDate>Tue, 28 Apr 2009 17:44:40 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-168895</guid>
		<description>The idea of structural programming is to have

* one point of entry in a block of code
* and one point of entry out.

As well as goto&#039;s, the following violates this idea

* multiple returns
* expection handling
* breaks
* continue

In the author&#039;s examples, there is one point of entry into the function and one point of entry out.  His goto&#039;s stay within the function.</description>
		<content:encoded><![CDATA[<p>The idea of structural programming is to have</p>
<p>* one point of entry in a block of code<br />
* and one point of entry out.</p>
<p>As well as goto&#8217;s, the following violates this idea</p>
<p>* multiple returns<br />
* expection handling<br />
* breaks<br />
* continue</p>
<p>In the author&#8217;s examples, there is one point of entry into the function and one point of entry out.  His goto&#8217;s stay within the function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Twylite</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-168852</link>
		<dc:creator>Twylite</dc:creator>
		<pubDate>Tue, 28 Apr 2009 12:35:47 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-168852</guid>
		<description>The general injunction against &quot;goto&quot; and multiple returns comes from a particular school of structured programming (Dijkstra) that shows that you can prove less about a program when you allow such flow control options.  This doesn&#039;t make them wrong - the programs may have fewer provable qualities, but may at the same time be more readable and thus more maintainable.

Oh, if you&#039;re avoiding goto and multiple returns because of Dijkstra&#039;s &quot;goto considered harmful&quot;, then you&#039;d better stop using exceptions too -- they violate his structural rules.

A do/while with breaks to jump to a cleanup section is a dirty hack to avoid using goto.  It keeps the structuralists happy because it doesn&#039;t technically violate their rules, but it is _more dangerous_ than goto.  Didn&#039;t know that, did you?  The C standard does not define semantics for break/continue in nested loops, so you cannot (safely, across compilers) use looping constructs within your error handling do/while construct.

Fortunately Knuth (who unlike Dijkstra DID program) wrote &quot;Structured Programming with Goto Statements&quot; and dispelled the idea that goto is always bad ... or at least he would have if people could grasp the idea of not pushing Dijkstra&#039;s goto myth all the time ...</description>
		<content:encoded><![CDATA[<p>The general injunction against &#8220;goto&#8221; and multiple returns comes from a particular school of structured programming (Dijkstra) that shows that you can prove less about a program when you allow such flow control options.  This doesn&#8217;t make them wrong &#8211; the programs may have fewer provable qualities, but may at the same time be more readable and thus more maintainable.</p>
<p>Oh, if you&#8217;re avoiding goto and multiple returns because of Dijkstra&#8217;s &#8220;goto considered harmful&#8221;, then you&#8217;d better stop using exceptions too &#8212; they violate his structural rules.</p>
<p>A do/while with breaks to jump to a cleanup section is a dirty hack to avoid using goto.  It keeps the structuralists happy because it doesn&#8217;t technically violate their rules, but it is _more dangerous_ than goto.  Didn&#8217;t know that, did you?  The C standard does not define semantics for break/continue in nested loops, so you cannot (safely, across compilers) use looping constructs within your error handling do/while construct.</p>
<p>Fortunately Knuth (who unlike Dijkstra DID program) wrote &#8220;Structured Programming with Goto Statements&#8221; and dispelled the idea that goto is always bad &#8230; or at least he would have if people could grasp the idea of not pushing Dijkstra&#8217;s goto myth all the time &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Martin</title>
		<link>http://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-c/comment-page-1/#comment-168787</link>
		<dc:creator>Daniel Martin</dc:creator>
		<pubDate>Tue, 28 Apr 2009 08:53:23 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1665#comment-168787</guid>
		<description>Hi, nice article, should cause a few people to actually engage their brain which can only be a good thing. Personally though I think I still prefer to have lots of nested IFs. It just doesn&#039;t feel too ugly to me. But hey, maybe this will fetser in the back of my mind and take over eventually!

(I don&#039;t even like exceptions that much.. - I guess I&#039;m a bit sick.)</description>
		<content:encoded><![CDATA[<p>Hi, nice article, should cause a few people to actually engage their brain which can only be a good thing. Personally though I think I still prefer to have lots of nested IFs. It just doesn&#8217;t feel too ugly to me. But hey, maybe this will fetser in the back of my mind and take over eventually!</p>
<p>(I don&#8217;t even like exceptions that much.. &#8211; I guess I&#8217;m a bit sick.)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
