{"id":20984,"date":"2013-09-06T12:57:06","date_gmt":"2013-09-06T17:57:06","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20984"},"modified":"2015-07-01T03:45:09","modified_gmt":"2015-07-01T08:45:09","slug":"project-good-robot-part-10-software-engineering","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20984","title":{"rendered":"Project Good Robot 10: Software Engineering"},"content":{"rendered":"<p>Sometimes people ask me what the difference is between a programmer and a software engineer, and my usual flippant answer is that software engineers are what programmers call themselves when they want to sound important. But that&#8217;s not entirely fair and often the distinction is worth making.<\/p>\n<p>Metaphorically, <em>software engineering<\/em> is designing a bridge that won&#8217;t fall over, and <em>programming<\/em> is the act of rolling up your sleeves and laying bricks or nailing boards together to make the bridge a reality. It&#8217;s easy to see the difference between the two disciplines when we talk about it in terms of construction, because you&#8217;ve usually got two very different kinds of people doing those jobs. The designer does her thing, then the builder does his, and the next thing you know you&#8217;ve got a bridge. But when you&#8217;re building software the same person is most likely doing both jobs, and they&#8217;re most likely doing them at the same time.<\/p>\n<p>Perhaps it sounds foolhardy to design a bridge while you build it. <a href=\"http:\/\/www.joelonsoftware.com\/articles\/fog0000000036.html\">Joel Spolsky will tell you<\/a> that you should design the bridge before anyone starts putting bricks on top of each other. And for the most part that&#8217;s true. Without good engineering you&#8217;ll either end up with ungainly code, or you end up doing lots of rewrites. <\/p>\n<p>But you can&#8217;t write a spec if you don&#8217;t know what what you want to build or how you want to build it. This isn&#8217;t a bridge-building project, this is a skunkworks.  We&#8217;re looking for fun gameplay and you can&#8217;t really appraise the fun of something until you&#8217;ve got a working prototype. <\/p>\n<p>So we&#8217;re coding with no spec. This leads to interesting problems. <\/p>\n<p><!--more--><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr10_bug.jpg' class='insetimage' width='600' alt='Most bugs are boring, but this one was funny: I was adding a feature to make robots fire in volleys instead of single shots.  I messed up the cooldown timing and the bot began shooting at me every single frame &#8211; 60 shots a second.' title='Most bugs are boring, but this one was funny: I was adding a feature to make robots fire in volleys instead of single shots.  I messed up the cooldown timing and the bot began shooting at me every single frame &#8211; 60 shots a second.'\/><\/td><\/tr><tr><td class='insetcaption'>Most bugs are boring, but this one was funny: I was adding a feature to make robots fire in volleys instead of single shots.  I messed up the cooldown timing and the bot began shooting at me every single frame &#8211; 60 shots a second.<\/td><\/tr><\/table><\/p>\n<p><em>Hm. Let&#8217;s see. I&#8217;m adding this code that will create this animated character for the player. All the controls are in player.cpp, so it makes sense to put this character-drawing stuff in there with it. I mean, one controls the <strong>position<\/strong> of the character and the other controls the <strong>appearance<\/strong> of the character. Those are basically related ideas, right?<\/em> <\/p>\n<p><em>Now what about this code that tracks player stats like shields, energy, XP, level, etc? Well, previously the only player data was the player position and momentum in player.cpp.  This level-tracking stuff is arguably just more player data, so it belongs in player.cpp.<\/em><\/p>\n<p><em>For testing purposes, I need to be able to see the player health. I&#8217;ll just have it print the raw values right to the screen. Just some quick-and-dirty rendering so I can get back to testing. <\/em><\/p>\n<p><em>Now I&#8217;m adding code to restrict what the player can see. Well, that ties into player position data, so I guess that should go into player.cpp.<\/em><\/p>\n<p><em>I need to see more than just player health. I need to see my supply of missiles and energy. And you know what would help? If I replaced these ugly numbers with proper status bars so I can get a feel for the values without needing to take my eyes off the action.<\/em><\/p>\n<p><em>I want to draw a blue sphere around the player when their shields go up or down. Oh, I can just attach that to the rendering of the player character here in player.cpp. <\/em><\/p>\n<p><em>The animated player character needs to generate particle effects as it flies around. That&#8217;s directly tied to the character, so that belongs in player.cpp.<\/em><\/p>\n<p><em>I need to handle input from the keyboard or the Xbox controller.  Well, all the controls are already in player.cpp, so this is a natural extension of that.<\/em><\/p>\n<p>Taken alone, any one of these decisions is pretty reasonable. But taken together, they are madness. One source file has inputs, game mechanics, particle effects, animation code, HUD rendering, line-of-sight checking, messing with the stencil buffer, polygon rendering, collision detection, mouse controls, and half a dozen other things. Going back to our bridge analogy: We&#8217;ve successfully built a structure that can let travelers cross the valley. But this is not a bridge, it&#8217;s a giant shapeless mound of bricks. It does what it needs to do, but with no sense of order, clarity, or efficiency.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr10_light1.jpg' class='insetimage' width='600' alt='As suggested in the comments, I restored the flashlight mode, but now the flashlight is purely cosmetic \/ psychological. You can see all around you, but where you&#8217;re aiming is brighter. I like it.' title='As suggested in the comments, I restored the flashlight mode, but now the flashlight is purely cosmetic \/ psychological. You can see all around you, but where you&#8217;re aiming is brighter. I like it.'\/><\/td><\/tr><tr><td class='insetcaption'>As suggested in the comments, I restored the flashlight mode, but now the flashlight is purely cosmetic \/ psychological. You can see all around you, but where you&#8217;re aiming is brighter. I like it.<\/td><\/tr><\/table><\/p>\n<p>So now that we&#8217;ve made a mess, let&#8217;s do some proper software engineering and give this thing some proper shape. (One advantage we have over bridge-builders is that we can sometimes get away with this sort of slapdash &#8220;build first, design second&#8221; mentality.) <\/p>\n<p>When you&#8217;re designing code you have to start thinking about interfaces. Not like, text boxes or buttons. I&#8217;m talking about programming interfaces. The whole point of cutting up your program into distinct sections is to manage complexity and try to control side-effects.<\/p>\n<p>Side-effects is a tough problem to explain. Let me take a stab at it:<\/p>\n<p>Let&#8217;s say I&#8217;m doing processing on a missile object in the game. I move it, check for collisions, and find it has just smacked into a bomb robot. So I pause processing this missile for a second to send a &#8220;you take damage&#8221; message to the robot. The robot&#8217;s health goes below zero.  This type of robot is supposed to explode when it does, so it spawns an explosion. The explosion initializes itself by looking at everything within its blast radius and sending it a damage packet. Let&#8217;s see&#8230; there&#8217;s the robot that spawned us, but he&#8217;s already dead.  Then we have the player, so let&#8217;s send them a damage packet. Oh! and there&#8217;s this missile in the blast radius. I&#8217;ll send it a damage packet.  The missile, upon getting the damage, sees that its hitpoints are below zero and so it destroys itself and deletes itself from memory.  <\/p>\n<p>Ok, great. What were we doing before all that? Oh, right. We were updating that missile. But isn&#8217;t that the missile we just deleted from memory? But that means-<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr10_crash.jpg' class='insetimage' width='600' alt='Would you like to check online for a solution to the crash bug you just created? No, thanks Microsoft. I&#8217;ve looked, and &#8220;online&#8221; is the worst place to look for those kinds of solutions.' title='Would you like to check online for a solution to the crash bug you just created? No, thanks Microsoft. I&#8217;ve looked, and &#8220;online&#8221; is the worst place to look for those kinds of solutions.'\/><\/td><\/tr><tr><td class='insetcaption'>Would you like to check online for a solution to the crash bug you just created? No, thanks Microsoft. I&#8217;ve looked, and &#8220;online&#8221; is the worst place to look for those kinds of solutions.<\/td><\/tr><\/table><\/p>\n<p>To be clear: This is a contrived and simplified example that will hopefully help you grasp this problem. I didn&#8217;t actually make this mistake. This would be a crash that would happen only when the player used a missile to kill a robot that exploded on death. Depending on how the game works, that might be hard to identify and track down. Players would just report &#8220;a crash&#8221;.  After a while someone might notice that the crash only happens when using missiles, but only sometimes. (If the blast radius of the missile is larger than the blast radius of the robot, then this crash will only happen sometimes.) Eventually you&#8217;ll be able to replicate the bug and follow this snarl of logic down to where things go wrong.<\/p>\n<p>When you&#8217;ve got a program with 40 different systems running (not a made up number in this case) the number of ways that different systems can interact becomes kind of terrifying. The most expedient way to solve the above problem would be for the missile to set a flag for itself saying, &#8220;I&#8217;m in the middle of processing, so don&#8217;t delete me.&#8221; That would fix this particular crash bug, but it wouldn&#8217;t fix the larger problem that these interfaces are badly designed and these systems don&#8217;t interact in a safe way. <\/p>\n<p>If you design the interface properly, then when you&#8217;re writing the explosion-making code you might notice that you can&#8217;t directly destroy other objects. All you can do is send them damage packets, which they will process in their own time, thus preventing large-scale interactions. <\/p>\n<p>You can take this idea even further, where individual objects are not allowed to change anything but themselves. This is what <a href=\"?p=20541\">John Carmack was talking about when he got into functional programming in his Quakecon keynote<\/a>. In a functional programming paradigm, an explosion couldn&#8217;t deal damage to other objects. Some super-object that owns both of them would need to offer a snapshot of the explosion to the missile saying, &#8220;Here is an explosion. Use it to damage yourself and let me know what happens to you.&#8221;<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr10_light2.jpg' class='insetimage' width='600' alt='Some people suggested that it should be pure darkness where your light isn&#8217;t shining. Here is what that looks like. It might be fun for a brief gameplay section, but I think this sort of thing would wear out its welcome quickly.' title='Some people suggested that it should be pure darkness where your light isn&#8217;t shining. Here is what that looks like. It might be fun for a brief gameplay section, but I think this sort of thing would wear out its welcome quickly.'\/><\/td><\/tr><tr><td class='insetcaption'>Some people suggested that it should be pure darkness where your light isn&#8217;t shining. Here is what that looks like. It might be fun for a brief gameplay section, but I think this sort of thing would wear out its welcome quickly.<\/td><\/tr><\/table><\/p>\n<p>It sounds pretty restrictive, and it is. But languages have gradually been getting more strict for decades. As we make larger and more complex software, we need ways to mitigate that complexity and make it comprehensible. In the old days we had imperative programming. (Also called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Procedural_programming\">procedural programming<\/a>.) I did imperative programming for over a decade before I even knew it was called that. There wasn&#8217;t a commonly used name for it because all languages were basically imperative. (At least in the areas where I operated. I never got near programming theorists or academics. I was just a kid figuring stuff out on his own, without the help of an internet.) <\/p>\n<p>The imperative approach to the above problem would have program execution jumping recklessly from missile-process to robot-processing to explosion-processing, where at any moment any system could make changes to any other. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\">Object-oriented design<\/a> built some walls around those systems and forces the programmer to deliberately place turnstile doorway for data moving in and out. An impatient programmer might be writing a bit of code to make an explosion do its thing and get annoyed that they can&#8217;t just directly destroy some other object. <em>Hey! Why can&#8217;t I call <code>missile-&gt;Destroy ()<\/code>? I know explosions ALWAYS do more than the base hitpoints of a missile, so there&#8217;s no need to waste time calling <code>missile-&gt;Damage (N)<\/code>. It will be way more efficient* to just add a way to destroy the missile directly.&#8221;<\/em><\/p>\n<p><small>* &#8220;Efficient&#8221; in this case means, &#8220;I have no idea how it might impact performance, but it&#8217;s way more convenient.<\/small><\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr10_caves.jpg' class='insetimage' width='600' alt='The lava level! The water level! The&#8230; lots-of-green-plants level! The purple one because shut up it&#8217;s cool!' title='The lava level! The water level! The&#8230; lots-of-green-plants level! The purple one because shut up it&#8217;s cool!'\/><\/td><\/tr><tr><td class='insetcaption'>The lava level! The water level! The&#8230; lots-of-green-plants level! The purple one because shut up it&#8217;s cool!<\/td><\/tr><\/table><\/p>\n<p>Object-oriented design lives or dies based on how smart your interface is. You can build a turnstile at every point in the wall, at which point the wall is no longer in your way but also no longer protecting you from dangerous levels of complexity. You&#8217;re effectively back to imperative programming, but with really screwy syntax. If you happen to get paid by lines of code, you might do okay for yourself. <\/p>\n<p>Functional languages make the wall impenetrable, and will only let you build ONE door. I wouldn&#8217;t want to program anything complicated like that, but it&#8217;s an interesting approach to what is clearly a growing problem: Eventually we won&#8217;t be smart enough to work on our own software. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes people ask me what the difference is between a programmer and a software engineer, and my usual flippant answer is that software engineers are what programmers call themselves when they want to sound important. But that&#8217;s not entirely fair and often the distinction is worth making. Metaphorically, software engineering is designing a bridge that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-20984","post","type-post","status-publish","format-standard","hentry","category-good-robot"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20984","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=20984"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20984\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}