{"id":35702,"date":"2016-12-20T06:00:56","date_gmt":"2016-12-20T11:00:56","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=35702"},"modified":"2016-12-20T09:18:33","modified_gmt":"2016-12-20T14:18:33","slug":"object-oriented-debate-part-3-damned-if-you-do","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=35702","title":{"rendered":"Object Oriented Debate Part 3: Damned if you do&#8230;"},"content":{"rendered":"<p>So the original problem is that programs turned into an incomprehensible mess. Too many disparate systems can make arbitrary changes to the state of the problem at any time.<\/p>\n<p>The solution we&#8217;ve been using (or trying to use) for the last couple of decades is to stick the various bits of our program into appropriately named objects, and then fit those objects into a hierarchy. <\/p>\n<p>If I have a SpaceMarine object then I should have a limited number of things that I can do to it. Perhaps Heal(), Damage(), or Spawn(), and so the inner workings of a SpaceMarine will be hidden from the outside world. This is called <i>encapsulation<\/i>. It keeps the complexity of the SpaceMarine from leaking out into the rest of the code, and it keeps the complexity of the rest of the code from polluting the SpaceMarine. <\/p>\n<p>Now let&#8217;s discuss some of the objections to OOP. Remember that this particular conversation was started by Brian Will in his video:<\/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\/QM1iUe6IofM\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=QM1iUe6IofM'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<p>But I don&#8217;t want to simply repeat what Will says, so I&#8217;m going to offer my own criticism of OOP. If you want to hear his arguments, watch the video.<\/p>\n<p><!--more--><strong>Problem One: Complete encapsulation is an unattainable ideal.<\/strong><\/p>\n<p>In my indie game <a href=\"http:\/\/www.shamusyoung.com\/goodrobot\/\">Good Robot<\/a>, enemy Robots can act upon each other. A robot might create other robots. It will both shove and be shoved by other robots that are crowding it. Robots can also create bullets. Robots can damage the player through direct contact.<\/p>\n<p>Bullets<span class='snote' title='1'>In this context, a &#8220;bullet&#8221; is any damaging projectile: Missiles, lasers, bombs, shotgun pellets, etc.<\/span> can destroy robots. Bullets can destroy each other when lasers shoot down missiles. Bullets can change their trajectory (homing missiles) based on the presence of robots. Bullets die when they strike doors, but they might also cause the door to open. Bullets can destroy machines that produce robots.<\/p>\n<p>Doors can act on robots and the player, preventing them from passing through. They block bullets. They open in response to player proximity. <\/p>\n<p>The player can create bullets. The player can be killed by bullets. The player alters the behavior of robots. The player alters the course of bullets. (Homing missiles again.) The player alters the behavior of doors. (It opens if they&#8217;re close enough.) <\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/Good-Robot-Weapon-5.gif' width=100% alt='Here are some bullets interacting with each other.' title='Here are some bullets interacting with each other.'\/><\/div><div class='mouseover-alt'>Here are some bullets interacting with each other.<\/div><\/p>\n<p>Keep in mind this is a simplified explanation of an already simple game. Once you&#8217;re talking about a game where players can control vehicles that have mounted weapons that can shoot projectiles that can destroy different vehicles that can cause explosions that will detonate barrels that can collapse physics objects that can crush players, you&#8217;re up against some serious challenges in enforcing simplicity on a system that&#8217;s inherently complex.<\/p>\n<p>Where is the encapsulation here? You can&#8217;t just shove bullets into a box and forget about them, because when bullets are updated they need to change the state of almost everything else in the gameworld. <b>That&#8217;s the whole point of bullets.<\/b> When you update bullets, a bunch of non-bullet things need to change, all across the program. You can fight this truth all you want, but in the end you haven&#8217;t really solved the original problem with procedural programming, which is that state interactions are complex and hard to understand. You&#8217;ve still got all of these interconnected systems changing one another at different times. You can&#8217;t hide their state away. Having objects A B and C all able to make changes to each other at various times is the goal of the program and the reason the problem was so hard to solve in the first place.<\/p>\n<p>A cheeky programmer might say something like, &#8220;Hey, robots and bullets are both just &#8220;things that move around and run into each other&#8221;. So let&#8217;s make them two different flavors of the same shared idea. Call them uh&#8230;  &#8220;flyers&#8221;. Then it&#8217;s just Flyers acting on other Flyers, and that technically doesn&#8217;t break encapsulation!<\/p>\n<p>But this is a semantic trick that doesn&#8217;t actually do anything to help us solve the problem. The program is exactly as complex as it was before. More importantly, bullets and robots are very different things and need to be handled in very different ways. Bullets are tiny, number in the hundreds, and live only for a few seconds. Robots number in the dozens and live for many seconds or even a few minutes. Both of these things need to be kept in separate lists, and those lists should be tightly packed in memory. They need to be drawn at different times in the render loop and they serve totally different purposes in the game. <\/p>\n<p>By mashing these two different ideas together, all you&#8217;ve done is made the nomenclature confusing and created a bunch of performance concerns.<\/p>\n<p><strong>2. Object hierarchies just replace one complexity with another.<\/strong><\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/ood_hierarchy.jpg' width=100% alt='Not to scale.' title='Not to scale.'\/><\/div><div class='mouseover-alt'>Not to scale.<\/div><\/p>\n<p>So next our enterprising coder will try a new approach. The point of OOP is to prevent different parts of the program from altering each other&#8217;s state in unpredictable ways. So what if we make a new object? We can call it &#8220;Scene&#8221; or something. It&#8217;s in charge of all the bullets, robots, doors, the player and anything else that might be doing rude things like changing the state of the game. Since the scene object owns all bullets and all robots, they are now part of its overall state. It&#8217;s not robots and bullets changing each other, it&#8217;s the Scene changing itself. And that adheres to OOP principles! Hooray!<\/p>\n<p>Of course, this solution is even worse than the last one. We&#8217;ve taken the code that controls the behavior of bullets and robots and pulled it out of their respective systems and into this huge container looming over them. Now if I want to see how bullets work I might need to look in Bullets.cpp, but maybe what I&#8217;m looking for is in Scene.cpp. <\/p>\n<p>The scene class will continue to bloat until it reaches gigantic proportions, because regardless of what you call it, conceptually this class is the class for &#8220;anything that might change the state of the game&#8221;. Sooner or later everything will end up inside of this thing and you&#8217;ll end up with all game logic controlled by a single class. You&#8217;ll have all the complexity problems of PP, plus you&#8217;ve got terribly organized code. <\/p>\n<p>Alternatively, the coder might create more and more classes for wrangling all of these types. A class to own bullets and robots. Another for doors and machines. Another for the static scenery. And then another class to manage all of <i>those<\/i>. All of those inter-object relationships can be mediated by these high-level abstract classes that only exist to allow these objects to talk to each other, rather than letting them simply talk to each other directly. As if making them talk through this third party will somehow simplify the inherent complexity of the problem. It&#8217;s an attempt to fix the problem through bureaucracy.<\/p>\n<p>These are just a few example. There are endless ways to impose OOP principles onto this problem. A programmer sees a problem where it&#8217;s really hard to keep track of state changes, and take it as a personal challenge to create some &#8220;pure&#8221; (yet impractical) OOP framework around it.  The problem is that we eventually lose sight of the goal, which is to create clear, efficient code. <\/p>\n<h3>There&#8217;s nothing wrong with OOP, you&#8217;re just bad at it!<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/ood_frustrated.jpg' width=100% alt='Have you tried trying harder?' title='Have you tried trying harder?'\/><\/div><div class='mouseover-alt'>Have you tried trying harder?<\/div><\/p>\n<p>When you point out that OOP results in a bunch of abstract classes, or that functionality ends up moved to non-intuitive places simply to satisfy OOP orthodoxy, the usual retort is that this won&#8217;t happen if you would just do it &#8220;right&#8221;. And here we come back to the problem of talking about coding as if it was a single profession and not a bunch of vastly different jobs that all share the same basic tools. An aerospace mechanic and an HVAC mechanic might both work on &#8220;machines&#8221; and they might both use wrenches, but neither one of them is qualified to tell the other how to do their job.<\/p>\n<p>Insisting that OOP works fine in all cases is like insisting that all machines can be fixed with a wrench because you&#8217;ve never encountered a problem that couldn&#8217;t be fixed with a wrench. It&#8217;s basically claiming that no problem could possibly exist that might be ill-suited to a strict OOP design. Instead the OOP apologist will insist that your problem &#8211; which they&#8217;ve never seen and know nothing about &#8211; could be solved by simply following their advice with the proper level of tenacity and blind faith.<\/p>\n<p>My problem isn&#8217;t really with OOP itself, but rather with the idea that its principles should be adhered to at all times without asking if it&#8217;s the right tool for the job. Clearly some problems lend themselves to one programming language better than another, and it&#8217;s reasonable to think that the same can be said for programming paradigms. It&#8217;s even reasonable to assume that in a large project, different parts warrant different approaches. <\/p>\n<h3>The Mountain Before Us<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/ood_mountain.jpg' width=100% alt='I just noticed that the specs say the goal is to climb the mountain, but it never says how high.' title='I just noticed that the specs say the goal is to climb the mountain, but it never says how high.'\/><\/div><div class='mouseover-alt'>I just noticed that the specs say the goal is to climb the mountain, but it never says how high.<\/div><\/p>\n<p>Let&#8217;s imagine an expedition tasked with climbing a mountain. <\/p>\n<p>Alan says, &#8220;Let&#8217;s go up this steep slope to reach the top.&#8221;<\/p>\n<p>Barbara replies, &#8220;That would be idiotic. That slope is too steep and dangerous. Half of us would die of exhaustion or injury before we made it.&#8221;<\/p>\n<p>Alan says, &#8220;Nonsense! Any slope can be overcome with sufficient physical training.&#8221;<\/p>\n<p>Barbara points to the road that winds back and forth up the side of the mountain. &#8220;We should go this way. The gradual slope will be far easier to overcome.&#8221;<\/p>\n<p>Alan shakes his head. &#8220;That road is really long. We don&#8217;t have enough food to make it.&#8221;<\/p>\n<p>&#8220;No! Any distance can be overcome if you just adhere to a strict rationing program.&#8221;<\/p>\n<p>And so these two dunces gainsay each other until they get frustrated and set off in different directions.<\/p>\n<p>Eventually, someone from Alan&#8217;s party staggers back and reports that the direct assault was a disaster. From now on, he&#8217;s a &#8220;road-and-rationing&#8221; man all the way.<\/p>\n<p>Meanwhile, someone like Brian Will crawls back to the base camp to report that they followed the all best practices for rationing and it was still horrific. People starved. And so he swears he&#8217;s never taking the path again. <\/p>\n<p>People arguing over which way is &#8220;right&#8221; are missing the key truth that both ways are technically &#8220;right&#8221; in the sense that you&#8217;ll get there and yet &#8220;wrong&#8221; in  the sense that you can&#8217;t get there without casualties. This is not an either \/ or question. It&#8217;s entirely possible that this mountain is simply too big to be overcome safely. There are limits to how much energy the human body can put out in a day and there are limits to how far rationing can take you, and this remains true no matter how vigorously you point out the flaws with the other person&#8217;s approach. Their entire argument is based on the faulty assumption that one of these two paths will lead to perfect success. <i>It clearly isn&#8217;t the other person&#8217;s, so it must be mine!<\/i><\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/ood_barkley.jpg' width=100% alt='Image taken from a documentary on <a href=&apos;https:\/\/en.wikipedia.org\/wiki\/Barkley_Marathons&apos;>The Barkley Marathons<\/a>, one of the most savage physical races ever devised. Entrants must run five laps around the 20-mile course, which features rough terrain, no navigation aids, no aid stations, and requires thousands of feet of climbing and descent. There&apos;s a documentary about it on Netflix, which is pretty good.' title='Image taken from a documentary on <a href=&apos;https:\/\/en.wikipedia.org\/wiki\/Barkley_Marathons&apos;>The Barkley Marathons<\/a>, one of the most savage physical races ever devised. Entrants must run five laps around the 20-mile course, which features rough terrain, no navigation aids, no aid stations, and requires thousands of feet of climbing and descent. There&apos;s a documentary about it on Netflix, which is pretty good.'\/><\/div><div class='mouseover-alt'>Image taken from a documentary on <a href='https:\/\/en.wikipedia.org\/wiki\/Barkley_Marathons'>The Barkley Marathons<\/a>, one of the most savage physical races ever devised. Entrants must run five laps around the 20-mile course, which features rough terrain, no navigation aids, no aid stations, and requires thousands of feet of climbing and descent. There's a documentary about it on Netflix, which is pretty good.<\/div><\/p>\n<p>We live in a world with finite money to spend on programmers with finite mental capacity to write code in a finite amount of time on hardware of finite power. Sometimes we run into problems that are really hard to solve. There will be challenges where the solution won&#8217;t fit neatly into theoretical systems designed by people who have never seen a problem like this before. Sometimes a problem might create unmanageable complexity regardless of what dogmas you cling to, because <em>some problems are really complicated<\/em>.<\/p>\n<p>In fact, we&#8217;re doomed to run into hard problems, because there&#8217;s no upper limit on how much stuff we want to accomplish. If you doubled the brainpower of every programmer on earth, then a lot of our current challenges would indeed be reduced to &#8220;trivial&#8221;. But by next week we&#8217;d be up against a new bunch of problems that had previously been clearly beyond us and are now just barely within reach. And then we&#8217;d end up right back here, arguing about how to think about problems that are too complicated to be thought about and trying to manage all of this expanding complexity.<\/p>\n<p>My point is that there are no simple answers or perfect techniques. Sometimes this job will be really hard and there&#8217;s nothing you can do about it. I&#8217;m okay with people like Brain Will who prefer a PP approach to authoring code. I&#8217;m okay with the folks who favor the OOP approach. What I&#8217;m wary of are the people who go around repeating dogma and insisting that all problems can be solved if you follow the One True Way. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>So the original problem is that programs turned into an incomprehensible mess. Too many disparate systems can make arbitrary changes to the state of the problem at any time. The solution we&#8217;ve been using (or trying to use) for the last couple of decades is to stick the various bits of our program into appropriately [&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-35702","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\/35702","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=35702"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/35702\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}