{"id":21,"date":"2014-05-18T08:54:08","date_gmt":"2014-05-18T08:54:08","guid":{"rendered":"https:\/\/adamvillalobos.com\/?p=21"},"modified":"2015-08-20T20:16:21","modified_gmt":"2015-08-20T20:16:21","slug":"coding-logic-versus-computer-code","status":"publish","type":"post","link":"https:\/\/adamvillalobos.com\/?p=21","title":{"rendered":"Logic versus Computer Code, Why pure functional programming is the future"},"content":{"rendered":"<p><strong>There and back again<\/strong><\/p>\n<p>Back when I was sitting in high school class bored with my TI-83 programming text based games, I realized I wanted to make something better. For me, better meant better graphics and faster execution. I wanted to make exciting action games.\u00a0The first thing I did was take my existing BASIC knowledge and try to output graphics as text using small almost pixel like characters.<\/p>\n<p>I ran into some obvious problems. It was slow and looked funny. I knew what I wanted the computer to do, it was just doing it poorly. I had to take the reins myself and tell the CPU what to do.\u00a0So, I began my adventure into z80 assembly. It was tough. I found myself printing out code on paper to work on it during class to find new ways to gain a few CPU cycles in my methods. I eventually wrote better faster games and learned more about CPUs. It provided me with a low level foundation I&#8217;ve used my entire career and even today I&#8217;m aware there is a certain respect for this knowledge. I was pretty happy about it and thought I was a great programmer. I was completely wrong.<\/p>\n<div id=\"attachment_32\" style=\"width: 202px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-32\" class=\"wp-image-32\" src=\"https:\/\/adamvillalobos.com\/wp-content\/uploads\/2014\/05\/70136.gif\" alt=\"70136\" width=\"192\" height=\"128\" \/><p id=\"caption-attachment-32\" class=\"wp-caption-text\">My only &#8216;released&#8217; TI-83 game in all its black and white glory.<\/p><\/div>\n<p>After awhile I started writing more complex programs and realized that it was difficult to write in assembly. After all I didn&#8217;t want to have to write a division\u00a0method every time I moved to a new system. When I started writing in C++, I started to realize how valuable the compiler was.\u00a0It took a &#8216;high level&#8217; operation like &#8220;a \/ b&#8221; and converted it to an optimal machine code in whatever system it needed to. I really appreciated things like &#8220;for&#8221; loops, &#8220;if&#8221; statements and even function calls. It provided me a better way to write my logic and let the computer take care of the hard work of figuring out how to optimally\u00a0execute it.<\/p>\n<p>Of course the compiler wasn&#8217;t perfect and as I started my career I found places people dropped back into assembly to tell the computer exactly what to do. I remember engineers\u00a0who would insist on manually inlining functions because of earlier compiler versions that would fail at this. Working\u00a0making video games there was also a certain pride from optimally managing memory pools and little respect for programmers using memory managed languages where they didn&#8217;t have to deal with this problem. I fell into this trap again and felt the hardest problem I had to solve was\u00a0telling the computer how to execute my code in an\u00a0optimal way.<\/p>\n<p>At the same time I was hitting bugs like I never had before. In my student projects I had some hard bugs, but it wasn&#8217;t a big deal. Bugs weren&#8217;t the end of the world.<\/p>\n<div id=\"attachment_34\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/adamvillalobos.com\/wp-content\/uploads\/2014\/05\/equanimity.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34\" class=\"size-medium wp-image-34\" src=\"https:\/\/adamvillalobos.com\/wp-content\/uploads\/2014\/05\/equanimity-300x166.png\" alt=\"All content \u00a9 2010 DigiPen (USA) \" width=\"300\" height=\"166\" srcset=\"https:\/\/adamvillalobos.com\/wp-content\/uploads\/2014\/05\/equanimity-300x166.png 300w, https:\/\/adamvillalobos.com\/wp-content\/uploads\/2014\/05\/equanimity.png 633w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-34\" class=\"wp-caption-text\">&#8220;Hey, the attack only works 50% of the time&#8221; &#8220;Huh, thats 50% more than I thought it would, ship it!&#8221;. Equanimity \u00a9 2010 DigiPen (USA)<\/p><\/div>\n<p>They didn&#8217;t cost thousands or millions of dollars. No one was laid off due to crashes. In the real world, I found myself working around the clock fixing critical issues.\u00a0It was about more than just my own job. Bugs and software failures had a massive impact on all the co-workers I had become close to. I almost burned out a few times\u00a0(maybe I actually did) and I started to think there had to be a better way. I looked at testing. I thought, &#8220;Maybe if I tested everything perfectly, I would never run into a bug again&#8221;.<\/p>\n<p>I looked at different applications and testing strategies and tried to apply them in\u00a0the area I had a passion for, video games. I became frustrated.\u00a0There were too many branches, too many mutating states, too many external systems. I could achieve &#8220;100% code coverage&#8221; and still not hit tons of critical bugs. It\u00a0seemed like insanity.<\/p>\n<p>Finally after a conversation with a friend, I found a beacon of light. Functional programming. Other developers had run into this problem too and made languages that\u00a0solved this. There was no more mutating memory and no more side effects. Finally, a system I could test. I looked\u00a0at these languages and thought\u00a0about writing\u00a0games in them. That was when I realized, while this would work, it would be incredibly slow to execute. I was back where I was when I started my journey.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Two Steps<\/strong><\/p>\n<p>Looking back, I missed the bigger picture in high school. The class I often spent the most time coding was my Math class. I remember thinking, &#8220;Well, this stuff is easy and simple. Why should I listen or look more into this when there are more practical and harder problems in my assembly code?&#8221;. What I missed is there are two parts of coding. One is the logic of what you want to do, the other is translating that logic to a language the computer understands. When I was writing the assembly games was I knew logically what I wanted to do. I could write it out in BASIC like I did before. What was hard wasn&#8217;t\u00a0the logic, it was telling the computer how to optimally execute my logic. Thats true for anything simple, but complex logic requires different tools.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The Language of Logic<\/strong><\/p>\n<p>Before computers, logic problems were solved\u00a0all the time. We&#8217;ve been devising a system for reasoning about these problems for thousands of\u00a0years called Mathematics. We may have some new systems to add to the mathematical language, but a large portion of it is very relevant to writing complex systems.<\/p>\n<p>Even many programming patterns and algorithms have a strong mathematical basis. Any time you hit a hard problem chances are someone already solved it years ago using math.<\/p>\n<p>Too often though, we get caught up in telling the CPU what to do. We forget to look at making sure what we&#8217;re trying to do makes sense.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The Translator<\/strong><\/p>\n<p>The process of taking the logic and changing it to machine code is called compiling.<\/p>\n<p>The reason programming languages are not able to fully separate from writing bits and bytes is that compilers are not quite ready. C\/C++ is a great example of this. The developer shouldn&#8217;t have to think about whether or not the compiler will inline a function. We&#8217;re getting better, but the reality is we&#8217;re not there yet.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The Future<\/strong><\/p>\n<p>If you look at most programming languages they operate on both of these concerns. They provide ways for you to write what you logically want to do and tell the computer\u00a0how to do it. This can be a big problem. Take the pseudo-code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">int x0 = 0;\r\nx0 = update(x0); \/\/adds 1\r\nmove(x0,y);<\/pre>\n<p>and<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">int x0 = 0;\r\nint x1 = update(x0); \/\/adds 1\r\nmove(x1,y);<\/pre>\n<p>Logically they both have the same result, the first one just tells the computer, &#8220;Hey, you can go ahead and put this result back at this address, because I&#8217;m done using\u00a0that object now&#8221;. While the second one says, &#8220;I might use x0 later, lets not delete it yet&#8221;. Given the rest of that function, the compiler should be able to determine\u00a0if x0 is used again and make the decision to re-use the memory as an optimization.<\/p>\n<p>Functional languages have started to become popular lately and there is a good reason for that. As we hit harder problems and break assumptions made about execution order, describing logically what we are doing has become more important. Functional languages are great at this as they stick to logic and provide ways of\u00a0describing that logic.<\/p>\n<p>Sadly,\u00a0we still have quite a bit of work to do on the compiler side before it can compete with low level languages. That being said, I&#8217;ve found it useful to prototype even systems with high performance requirements in functional languages to make sure my logic is sound before moving into other languages. In fact, I think everyone should learn a functional language. Its a bit easier than going back to your math books and trying to piece together how category theory helps you write a web server.<\/p>\n<p>It will be an interesting change, but maybe in 10-20 years the idea of large amounts of coding in languages that mix logic and computer code will seem as crazy as writing large amount of assembly code does now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There and back again Back when I was sitting in high school class bored with my TI-83 programming text based games, I realized I wanted to make something better. For <a href=\"https:\/\/adamvillalobos.com\/?p=21\">&#8230;Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":10,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adamvillalobos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}