<?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: error handling</title>
	<atom:link href="http://blogs.gnome.org/otte/2008/02/04/error-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/</link>
	<description>Just another GNOME Blogs weblog</description>
	<lastBuildDate>Fri, 20 Nov 2009 09:10:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: http://tyoc.nonlogic.org/</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-183</link>
		<dc:creator>http://tyoc.nonlogic.org/</dc:creator>
		<pubDate>Sat, 09 Feb 2008 18:08:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-183</guid>
		<description>I recommend also this discussion: http://ubuntuforums.org/showthread.php?t=631745 I wrote that the name for that is &quot;coding in possitive and to the point&quot;.

See this:

[code]
boolean file_get_contents (const char *filename, char **data, size_t *len)
{
  ssize_t len;
  char *data;
  int fd;

  fd = open (filename, O_RDONLY);
  if (fd &gt; 0){
    len = get_length (fd); // magic function
    if(len &gt; 0){
      data = malloc (len);
      if(data){
        if(read(fd, data, len) == len){
          file_close (f); // you dont need to check &quot;f&quot;, becaus already pass
          *data_out = data;
          *len_out = len;
          return TRUE;
        }
      }
    }
  }
  *data_out = NULL;
  *len_out = 0;
  return FALSE;
}
[/code]

Simple and to the point, no extra hided jumps (at opcode or processor level -&gt;yea the check for errors and return or jump to else clauses).

I consider a very bad practice check for errors in the way that all people do, and redirect us to post/discussions like this one, and the one linked in the ubuntuforums.

The code being possitive (check for success not failure) and to the point (continue until you know you are right, is easy, because you dont need to ask each time &quot;I have failed?&quot; if you know you have not fail, thus when you fail you can start thinking why you have fail).

The good about this is that you can code in this way whatever the problem is, for your second example:


[code]
boolean
file_get_contents (const char *filename, char **data_out, size_t *len_out)
{
  File *f = file_open (filename); // returns a preallocated &quot;error&quot; object on OOM
  // I dont know why you didnt check f, thus I dont check it
  size_t len = file_get_length (f);
  char *data = malloc (len);
  if (data) {
    file_read (f, &amp;data, &amp;len);
    file_close (f);
    if (!file_in_error (f)) {
      *data_out = data;
      *len_out = len;
      file_destroy (file);
      return TRUE;
    }
    free(data); // You havent liberated the memory on fail!!! if your file i in error!
  }
  *data_out = NULL; // See the commend above
  *len_out = 0;
  file_destroy (file);
  return FALSE;
}
[/code]
Also</description>
		<content:encoded><![CDATA[<p>I recommend also this discussion: <a href="http://ubuntuforums.org/showthread.php?t=631745" rel="nofollow">http://ubuntuforums.org/showthread.php?t=631745</a> I wrote that the name for that is &#8220;coding in possitive and to the point&#8221;.</p>
<p>See this:</p>
<p>[code]<br />
boolean file_get_contents (const char *filename, char **data, size_t *len)<br />
{<br />
  ssize_t len;<br />
  char *data;<br />
  int fd;</p>
<p>  fd = open (filename, O_RDONLY);<br />
  if (fd &gt; 0){<br />
    len = get_length (fd); // magic function<br />
    if(len &gt; 0){<br />
      data = malloc (len);<br />
      if(data){<br />
        if(read(fd, data, len) == len){<br />
          file_close (f); // you dont need to check "f", becaus already pass<br />
          *data_out = data;<br />
          *len_out = len;<br />
          return TRUE;<br />
        }<br />
      }<br />
    }<br />
  }<br />
  *data_out = NULL;<br />
  *len_out = 0;<br />
  return FALSE;<br />
}<br />
[/code]</p>
<p>Simple and to the point, no extra hided jumps (at opcode or processor level -&gt;yea the check for errors and return or jump to else clauses).</p>
<p>I consider a very bad practice check for errors in the way that all people do, and redirect us to post/discussions like this one, and the one linked in the ubuntuforums.</p>
<p>The code being possitive (check for success not failure) and to the point (continue until you know you are right, is easy, because you dont need to ask each time &#8220;I have failed?&#8221; if you know you have not fail, thus when you fail you can start thinking why you have fail).</p>
<p>The good about this is that you can code in this way whatever the problem is, for your second example:</p>
<p>[code]<br />
boolean<br />
file_get_contents (const char *filename, char **data_out, size_t *len_out)<br />
{<br />
  File *f = file_open (filename); // returns a preallocated "error" object on OOM<br />
  // I dont know why you didnt check f, thus I dont check it<br />
  size_t len = file_get_length (f);<br />
  char *data = malloc (len);<br />
  if (data) {<br />
    file_read (f, &amp;data, &amp;len);<br />
    file_close (f);<br />
    if (!file_in_error (f)) {<br />
      *data_out = data;<br />
      *len_out = len;<br />
      file_destroy (file);<br />
      return TRUE;<br />
    }<br />
    free(data); // You havent liberated the memory on fail!!! if your file i in error!<br />
  }<br />
  *data_out = NULL; // See the commend above<br />
  *len_out = 0;<br />
  file_destroy (file);<br />
  return FALSE;<br />
}<br />
[/code]<br />
Also</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dvk</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-175</link>
		<dc:creator>dvk</dc:creator>
		<pubDate>Tue, 05 Feb 2008 10:16:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-175</guid>
		<description>That&#039;s why there are exceptions and more general ways to handle errors like lisp&#039;s conditions/restarts. They all allow to fail fast and loud and not to have resources leaked.</description>
		<content:encoded><![CDATA[<p>That&#8217;s why there are exceptions and more general ways to handle errors like lisp&#8217;s conditions/restarts. They all allow to fail fast and loud and not to have resources leaked.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Havoc Pennington: Out-of-memory Handling - D-Bus Experience</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-174</link>
		<dc:creator>Havoc Pennington: Out-of-memory Handling - D-Bus Experience</dc:creator>
		<pubDate>Tue, 05 Feb 2008 01:34:58 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-174</guid>
		<description>[...] started a blog thread about handling [...]</description>
		<content:encoded><![CDATA[<p>[...] started a blog thread about handling [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bjarne</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-173</link>
		<dc:creator>Bjarne</dc:creator>
		<pubDate>Mon, 04 Feb 2008 21:38:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-173</guid>
		<description>Dude, learn a language with exception handling!
You really should check for errors. Just try running your code with  zzuf (http://sam.zoy.org/zzuf/) to see what can go wrong.</description>
		<content:encoded><![CDATA[<p>Dude, learn a language with exception handling!<br />
You really should check for errors. Just try running your code with  zzuf (<a href="http://sam.zoy.org/zzuf/" rel="nofollow">http://sam.zoy.org/zzuf/</a>) to see what can go wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abstract nonsense &#187; Blog Archive &#187; About error handling</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-172</link>
		<dc:creator>Abstract nonsense &#187; Blog Archive &#187; About error handling</dc:creator>
		<pubDate>Mon, 04 Feb 2008 20:40:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-172</guid>
		<description>[...] cases embedded in them. Typical examples are functions which return a NULL pointer on error, or how cairo does things, with a more general error handling (basically, the result object has some kind of [...]</description>
		<content:encoded><![CDATA[<p>[...] cases embedded in them. Typical examples are functions which return a NULL pointer on error, or how cairo does things, with a more general error handling (basically, the result object has some kind of [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: buz</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-171</link>
		<dc:creator>buz</dc:creator>
		<pubDate>Mon, 04 Feb 2008 19:17:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-171</guid>
		<description>Or we could just use stack based exceptions ;)</description>
		<content:encoded><![CDATA[<p>Or we could just use stack based exceptions ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mieo</title>
		<link>http://blogs.gnome.org/otte/2008/02/04/error-handling/comment-page-1/#comment-170</link>
		<dc:creator>mieo</dc:creator>
		<pubDate>Mon, 04 Feb 2008 19:17:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/otte/2008/02/04/error-handling/#comment-170</guid>
		<description>Hi,

I&#039;m sorry but this is wrong. You should *always* check for errors. Always. If the file is larger that 2GB? If the code is used in a device with small (non swap) memory?

Also reusing objects for error status, makes them even harder to use.

Just make all your functions return a mandatory int which would be the last error and stick to it.

Yes it is more code (use c++, java if you want exceptions), but it is 
a) readable
b) dependable
c) stateless

It might be good idea to use the cairo way, but this is rather specific (high performance) scenario.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;m sorry but this is wrong. You should *always* check for errors. Always. If the file is larger that 2GB? If the code is used in a device with small (non swap) memory?</p>
<p>Also reusing objects for error status, makes them even harder to use.</p>
<p>Just make all your functions return a mandatory int which would be the last error and stick to it.</p>
<p>Yes it is more code (use c++, java if you want exceptions), but it is<br />
a) readable<br />
b) dependable<br />
c) stateless</p>
<p>It might be good idea to use the cairo way, but this is rather specific (high performance) scenario.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
