{"id":26046,"date":"2015-03-01T14:49:29","date_gmt":"2015-03-01T19:49:29","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26046"},"modified":"2015-03-01T14:50:15","modified_gmt":"2015-03-01T19:50:15","slug":"a-new-programming-language-for-games-annotated-part-3","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26046","title":{"rendered":"A new programming language for games, Annotated: Part 3"},"content":{"rendered":"<p>The annotation continues&#8230;<\/p>\n<p><table class='nomargin' cellspacing='0' width='100%' cellpadding='0' align='center' border='0'><tr><td><iframe loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/www.youtube.com\/embed\/TH9VCN6UkyQ\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=TH9VCN6UkyQ'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<h3>39:00 Exceptions Are Silly.<\/h3>\n<p>Exceptions are where you run a bit of code, but you sort of leave a note for the program, &#8220;If anything goes wrong, jump to this bit of code and spit out such-and-such message.&#8221; <a href=\"http:\/\/channel9.msdn.com\/Shows\/Going+Deep\/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C\">Here is the video Blow mentioned<\/a>, which talks about why exceptions are such a mess.<\/p>\n<p>My experience with exceptions is pretty limited. In my professional work, we used a large codebase that began life in 1994 as vanilla C. Somewhere around 2000-ish we migrated (in fits and starts) to C++. So I didn&#8217;t even have a chance to use exceptions until 2000, and we didn&#8217;t really have a lot of need for them. Our software was pretty mature and we already had all the error handling we needed.<\/p>\n<p><!--more-->As Blow says, in the old C days, there was just this one global variable (a variable available from anywhere in the program) that you could check. &#8220;Hey, has anything gone wrong recently?&#8221; And then it would reply &#8220;532&#8221;, and you&#8217;ve have to stop programming and find some documentation that told you what error 532 was. <\/p>\n<p>I really don&#8217;t see the use of exceptions. I&#8217;m not saying they&#8217;re useless. I&#8217;m just saying I haven&#8217;t run into any cases where I thought to myself, &#8220;Man, throwing an exception would really solve this problem for me.&#8221; This might be due to the toolset I use. The best case I&#8217;ve heard for throwing exceptions is to use them during development. But in the programming environment I use, I can just set a breakpoint:<\/p>\n<pre lang=\"c\" line=\"1\">\r\nDoStuff ();\r\nif (something awful happens) {\r\n  int nothing;\r\n}\r\nDoMoreStuff ();\r\n<\/pre>\n<p>Let&#8217;s say that &#8220;something awful&#8221; is whatever circumstances would cause you to throw an exception. Instead, I stick a breakpoint on line 3. If execution ever reaches there, my program pauses and Visual Studio pops to the front. I can see the state of every variable in play, and I can even retrace the steps the program took to reach this unfortunate outcome. I can have the program advance, one line of code at a time, and watch the variable change as I go. This is a huge part of my workflow, and I honestly don&#8217;t know how I&#8217;d live without it. <\/p>\n<p>With a system like this, I just can&#8217;t imagine what benefit I&#8217;d ever get from building robust blocks of code to throw and catch detailed exceptions. <\/p>\n<h3>55:00 &#8220;Stud Unique Putter&#8221;.<\/h3>\n<p>I don&#8217;t have a lot to say about this section in general, but this is a good excuse to talk about code beauty. So let&#8217;s use Blow&#8217;s example:<\/p>\n<pre lang=\"c\" line=\"1\">\r\nstd::unique_ptr<Vector3>  *positions;\r\nstd::unique_ptr<int>      *index_list;\r\n<\/pre>\n<p>&#8230;is indeed really ugly. I realize it&#8217;s all dumb gibberish to non-coders, but once you know code you come to appreciate brevity and clarity, and this is neither. It&#8217;s messy, verbose, and unclear. Some of this is inherited from C. Here is how you want your program to look:<\/p>\n<pre lang=\"c\" line=\"1\">\r\nstring        name;\r\nint           hitpoints;\r\nint           dexterity;\r\nbool          is_dead;\r\n<\/pre>\n<p>So I&#8217;ve made four variables. Name is a chunk of text presumably holding the character name. Hitpoints is an integer. Dexterity is an integer. is_dead is a bool, which is a value that can only be true or false. <\/p>\n<p>So far so good.<\/p>\n<p>But you can also make pointers. If I have a pointer to a string, then what I&#8217;ve really got is an address of memory to go and get the text from. Sometimes you need to do this. So maybe you have a thing, or maybe you have a pointer <strong>to<\/strong> a thing. Maybe you have your dry cleaning, or maybe you just have the ticket with the address of where you can go to pick up your dry cleaning. In C, it&#8217;s done this way:<\/p>\n<pre lang=\"c\" line=\"1\">\r\nstring        name;\r\nint           hitpoints;\r\nint           dexterity;\r\nbool          *is_dead;\r\n<\/pre>\n<p>In this example, is_dead is now a pointer. (I have no idea why you would do things like this, but I&#8217;m trying to make a clear example. Just go with it.) That little asterisk tells you that is_dead is a pointer to a bool, instead of being a bool. But for the purposes of clarity, we like to keep the code neat. You can see in my example code that we have the types on the left and the variable names on the right. But now this asterisk is a little mysterious. It&#8217;s part of the type, but it goes with the variable name. If I do this:<\/p>\n<pre lang=\"c\" line=\"1\">\r\nbool*          is_dead, is_stunned;\r\n<\/pre>\n<p>Even after all my years of experience, I still look at this code and think it should mean that is_dead and is_stunned are pointers. But the asterisk only applies to is_dead. I&#8217;d need to throw another asterisk just before is_stunned if I wanted it to also be a pointer. Our desire to organize code is at odds with this one oddball symbol, and coding conventions are invented to protect against these kinds of mistakes. (The one I followed for years was &#8220;no more than one variable defined per line&#8221;. And it existed to deal with exactly this problem.)<\/p>\n<p>You can get used to  stuff like this<span class='snote' title='1'>I mean, SOME people do.<\/span>. But if you&#8217;ve ever tried to teach somebody the language and found yourself apologizing for strange stuff that can&#8217;t be intuited from existing rules then you know how ugly this feels. It&#8217;s a bit like this XKCD:<\/p>\n<p><table width='285'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><a href='http:\/\/xkcd.com\/1479\/'><img src='http:\/\/imgs.xkcd.com\/comics\/troubleshooting.png' class='insetimage' width='285' alt='&#8221;Oh, you&#8217;re using their Chrome APP, not their Chrome EXTENSION. They&#8217;re very similar but one handles window creation differently.&#8221; is a thing I hope I can stop saying soon.' title='&#8221;Oh, you&#8217;re using their Chrome APP, not their Chrome EXTENSION. They&#8217;re very similar but one handles window creation differently.&#8221; is a thing I hope I can stop saying soon.'\/><\/a><\/td><\/tr><\/table><\/p>\n<p>The code Blow is showing us is a serious problem at the other extreme, where a big long mess of syntax is used to express something that really ought to be simple. <\/p>\n<p>For the record, &#8220;std::unique_ptr&#8221; is short<span class='snote' title='2'>Not that it&#8217;s actually all that short.<\/span> for &#8220;standard library, unique pointer&#8221;. <\/p>\n<p>We&#8217;ll wrap this series up in the next entry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The annotation continues&#8230; Link (YouTube) 39:00 Exceptions Are Silly. Exceptions are where you run a bit of code, but you sort of leave a note for the program, &#8220;If anything goes wrong, jump to this bit of code and spit out such-and-such message.&#8221; Here is the video Blow mentioned, which talks about why exceptions are [&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-26046","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\/26046","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=26046"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/26046\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}