{"id":59,"date":"2008-06-06T03:29:33","date_gmt":"2008-06-06T08:29:33","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/06\/06\/case\/"},"modified":"2008-06-06T03:29:34","modified_gmt":"2008-06-06T08:29:34","slug":"case","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/06\/06\/case\/","title":{"rendered":"case:"},"content":{"rendered":"<p>This bug was frustrating enough that half-way through squashing it, I promised myself I would document the solution.  Unfortunately, it&#8217;s not a particularly interesting bug, but a promise is a promise!<\/p>\n<p>I had some code very much like:<br \/>\n<code><br \/>\ntypedef enum {NoError, ReadError, WriteError, NetworkError, UnknownError} ErrorType;<br \/>\n...<br \/>\nvoid DescriptionOfError(ErrorType err, char *string)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;switch(err)<br \/>\n&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IOError:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(string, \"Could not read data.\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WriteError:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(string, \"Could not write data!\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NetworkError:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(string, \"Network Error.  Make sure you have an internet connection and try again\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NoError:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(string, \"No error.\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UnknownError:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(string, \"Unknown error.\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/p>\n<p>int main (int argc, char** argv)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;char desc[1024];<br \/>\n&nbsp;&nbsp;&nbsp;DescriptionOfError(ReadError,desc);<br \/>\n&nbsp;&nbsp;&nbsp;printf(\"error: %s\\n\", desc);<br \/>\n&nbsp;&nbsp;&nbsp;return 0;<br \/>\n}<br \/><\/code><\/p>\n<p>And it just wouldn&#8217;t do the right thing, but for the life of me I couldn&#8217;t see what was wrong.<\/p>\n<p>The &#8220;solution&#8221; was very simple, and somewhat embarrassing.  I forgot the <code><strong>case<\/strong><\/code> keyword before the labels in the <code>switch<\/code> statement.  Turns out that if you don&#8217;t have a <code>case<\/code> in-front of a label, it&#8217;s treated like a <code>goto<\/code> label, not a <code>switch<\/code> label.  And this is something that I&#8217;ve known for years, but for 20 minutes I kept <em>reading<\/em> the code, and my brain would interpret what it should say, not literally analyze it.<\/p>\n<p>This was <em>extremely frustrating<\/em>, not because it took me a long time to fix (I wish all my bugs could be squashed in just 20 minutes!), but because <a href=\"http:\/\/vgable.com\/blog\/2008\/06\/03\/applescript-is-the-uncanny-valley\/\">the results I was seeing totally violated my mental model of what should have happened.<\/a>  Violating someone&#8217;s mental model is more unsettling then you might imagine &#8212; avoid doing it at all costs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This bug was frustrating enough that half-way through squashing it, I promised myself I would document the solution. Unfortunately, it&#8217;s not a particularly interesting bug, but a promise is a promise! I had some code very much like: typedef enum {NoError, ReadError, WriteError, NetworkError, UnknownError} ErrorType; &#8230; void DescriptionOfError(ErrorType err, char *string) { &nbsp;&nbsp;&nbsp;switch(err) &nbsp;&nbsp;&nbsp;{ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,7,6,12,5,4],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-cocoa","category-design","category-objective-c","category-programming"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}