{"id":24568,"date":"2014-10-01T11:39:13","date_gmt":"2014-10-01T16:39:13","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=24568"},"modified":"2014-10-01T23:13:31","modified_gmt":"2014-10-02T04:13:31","slug":"pleasant-and-helpful-error-messages","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=24568","title":{"rendered":"Pleasant and helpful error messages"},"content":{"rendered":"<p>This was originally a commentary on <a href=\"https:\/\/www.youtube.com\/watch?v=TH9VCN6UkyQ\" title=\"Ideas about a new programming language for games\">the talk by Jon Blow about creating a programming language designed specifically for games<\/a>. At one point he mentions &#8220;Pleasant and helpful error messages&#8221; and I got caught up thinking about what that would really entail. So let&#8217;s talk about compiler errors.<\/p>\n<p>Compilers are very bad at giving us useful error messages. I&#8217;ve been doing this for decades and I still get errors that baffle me. You could make the case that &#8220;better error messaging&#8221; could be a whole project in itself. You could keep yourself pretty busy by just ditching the whole &#8220;new language&#8221; idea and just attempting to give the C++ compiler more useful output. (Although that&#8217;s probably a bad idea, for reasons I&#8217;ll talk about below.)<\/p>\n<p>There are errors that don&#8217;t make sense and point to things that aren&#8217;t the source of the problem. They also lean really heavy on the jargon. This is a subject near and dear to my heart. I mean, this article exists because I have this compulsion to help other people understand difficult things. <\/p>\n<p>Lots of people point to <a href=\"http:\/\/yosefk.com\/c++fqa\/templates.html\" title=\"What's the idea behind templates?\">templates<\/a> and classes as a source of baffling messages. But rather than dive into the deep parts of the language or pick on some goofy obscure edge-case, let&#8217;s look at a really simple error:<\/p>\n<p><!--more--><\/p>\n<pre lang=\"c\" line=\"1\">\r\n  float speed;\r\n\r\n  speed = DoMovement ();\r\n  if (speed >> 8.0f)\r\n    speed = 8.0f;\r\n<\/pre>\n<p>So DoMovement () sets our speed (don&#8217;t ask me what this program does) and then we attempt to limit that speed by not allowing it to go above 8. The problem here is on line 4. There should only be one &gt; symbol there. By putting two, the compiler thinks I&#8217;m trying to bit-shift speed by eight places<span class='snote' title='1'>That is, move all its binary digits &#8211; all those 1&#8217;s and 0&#8217;s &#8211; to the right. It&#8217;s like moving the decimal point in a regular number.<\/span> instead of compare it to the number eight. This generates four errors:<\/p>\n<p><code>IntelliSense: expression must have integral or unscoped enum type<br \/>\nIntelliSense: expression must have integral or unscoped enum type<br \/>\nerror C2297: '>>' : illegal, right operand has type 'float'<br \/>\nerror C2296: '>>' : illegal, left operand has type 'float'<br \/>\n<\/code><\/p>\n<p>Okay, the first two errors come from <a href=\"http:\/\/en.wikipedia.org\/wiki\/Intelligent_code_completion\">IntelliSense<\/a> and not the compiler itself. (I think.) But that doesn&#8217;t matter for the purposes of this discussion. The point is that these messages are a very odd and obtuse way to explain these errors. This is not how a human being would describe these problems, unless they were deliberately trying to annoy you. It&#8217;s like someone saying, &#8220;The fuel reservoir has insufficient material to enable combustion&#8221; instead of saying &#8220;Your car is out of gas&#8221;. Yes, a programmer who knows what they&#8217;re doing should be able to parse that, but it&#8217;s not an optimal way to communicate. If nothing else, it&#8217;s needlessly <em>longer<\/em>.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/preview_octant.jpg' class='insetimage' width='600' alt='Image unrelated.' title='Image unrelated.'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;m betting some coders will tell you &#8220;It&#8217;s not the compiler&#8217;s job to teach you how to program,&#8221; and &#8220;You&#8217;ll learn to recognize the error messages eventually.&#8221; But I don&#8217;t think it really works that way. I very rarely <strong>look at<\/strong> the error messages to see what I did wrong.  They&#8217;re verbose and difficult. I just double-click on the error and examine the line of code it takes me to. I don&#8217;t read the message because it&#8217;s usually a waste of time and I can spot the problem myself faster than I can untangle the addled compiler output. It&#8217;s not that experienced programmers learn to understand the compiler better, it&#8217;s that we get enough experience that we no longer need to read its output to spot problems. <\/p>\n<p>I don&#8217;t know. Maybe it&#8217;s me, but I never find much value in the message except to tell me where the problem is. Consider the above error:<\/p>\n<p><code>error C2297: '>>' : illegal, right operand has type 'float'<\/code><\/p>\n<p>A human would probably say, &#8220;You can&#8217;t bit-shift with float values.&#8221; That&#8217;s probably <em>too<\/em> informal for a compiler, but it&#8217;s closer to useful than the one we have now. When I parse this, I stop reading when I see the phrase &#8220;right operand&#8221; and go back to looking at the code. The error message could be changed to &#8220;the right operand is a dumb butt-head loser&#8221; and I probably wouldn&#8217;t even notice.<\/p>\n<p>The above example is a terrible message because it doesn&#8217;t explain what it THINKS you&#8217;re trying to do, and it doesn&#8217;t say <em>why<\/em> the operation is illegal. If you&#8217;re still thinking that you only typed one &gt; symbol, then you&#8217;ll be baffled. <em>What? It&#8217;s illegal to compare a float? Since when?<\/em> If the phrase &#8220;bit-shift&#8221; was in there you&#8217;d be much more likely to notice your typo, because it would draw attention to the operator instead of the operand.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/preview_frontier.jpg' class='insetimage' width='600' alt='Image unrelated.' title='Image unrelated.'\/><\/td><\/tr><\/table><\/p>\n<p>Having said all this, I&#8217;ll admit it&#8217;s a tough problem to solve. I don&#8217;t know who composes error messages now, but I&#8217;m willing to bet that:<\/p>\n<ol>\n<li>They know the language very, very well. So well that really complex stuff can seem trivially &#8220;obvious&#8221; to them.\n<li>They are an engineer, and were chosen for their technical prowess, not for their communications skills.\n<\/ol>\n<p>It would take a ton of work, and it&#8217;s not clear what the gain would be in a purely productivity standpoint. Just saying, &#8220;It would be nicer and I would like it more&#8221; sounds like a weak justification for making sweeping changes to compiler output. It&#8217;s certainly not something people would tolerate in a mature language like C++. Giving the compiler &#8220;more friendly&#8221; error messages now would probably do more harm than good. Sure, the inexperienced would have an easier time, but the mature coders &#8211; the people currently doing real work &#8211; might have their workflow disrupted. (Unless they&#8217;re like me and read the code instead of the error.) And that&#8217;s never a worthwhile trade-off.<\/p>\n<p>But I think &#8220;more readable errors&#8221; is a noble goal for a new language. <\/p>\n<p>If you want to get all new-age touchy-feely, you could even have errors with optional expanded explanations that link to the language&#8217;s wiki. And then you really could have a compiler that could teach you the language. And that would be pretty cool.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This was originally a commentary on the talk by Jon Blow about creating a programming language designed specifically for games. At one point he mentions &#8220;Pleasant and helpful error messages&#8221; and I got caught up thinking about what that would really entail. So let&#8217;s talk about compiler errors. Compilers are very bad at giving us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-24568","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/24568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24568"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/24568\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}