{"id":48554,"date":"2019-11-21T06:00:14","date_gmt":"2019-11-21T11:00:14","guid":{"rendered":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=48554"},"modified":"2019-11-21T03:54:44","modified_gmt":"2019-11-21T08:54:44","slug":"programming-vexations-part-11-the-schism","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=48554","title":{"rendered":"Programming Vexations Part 11: The Schism"},"content":{"rendered":"<p>There&#8217;s a bit of a schism in the world of programming. This divide isn&#8217;t over a single issue, but instead over a sort of emerging design philosophy that tends to cluster around particular ideas. It&#8217;s complex and multifaceted, and you could spend an entire book exploring all the various differences. In the broad strokes people talk about it in terms of being a debate between people who favor object oriented (OO) programming and people who favor data-oriented (DO) programming, but that&#8217;s mostly shorthand for a lot of competing ideas.<\/p>\n<p>A couple of years ago I wrote about <a href=\"?p=35678\">object-oriented programming<\/a>, and the criticisms people have with it. Since then, that discussion has grown louder and more complex. What I&#8217;m about to outline is a simplification of this ongoing debate.<\/p>\n<p>Yes, there are other programming styles \/ philosophies besides these two, but let&#8217;s ignore them for now because they&#8217;re not really relevant to game development.<\/p>\n<p><!--more--><\/p>\n<h3>The Schism<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/vex_tug_of_war.jpg' width=100% alt='Ironically, the goal of the project is to build a system that keeps people from falling into the hole.' title='Ironically, the goal of the project is to build a system that keeps people from falling into the hole.'\/><\/div><div class='mouseover-alt'>Ironically, the goal of the project is to build a system that keeps people from falling into the hole.<\/div><\/p>\n<p>OO design is a design philosophy that dictates how your code should be structured \/ conceptualized, while DO design is a set of priorities that says what your code should <b>do<\/b>. A DO programmer might use OO design patterns to solve their DO problems, so these two camps aren&#8217;t <b>necessarily<\/b> at odds. And of course people participating in the debate are often ignoring the all-important context of the domain they&#8217;re working in. A user interface programmer will insist that OO design is the way to go, and an embedded systems<span class='snote' title='1'>If you&#8217;re working on embedded systems, you&#8217;re designing control systems for hardware rather than writing something a user interacts with directly. Think drones and comm satellites.<\/span> developer will claim that OO is madness and DO design is the only sane way to go, but in truth they&#8217;re both using systems that fit their particular job.<\/p>\n<p>For the purposes of this discussion, I&#8217;m going to refer to these two sides as Object People and Data People, because the OO and DO acronyms look silly. There&#8217;s no way I could read the previous paragraph aloud without snickering.<\/p>\n<p>Here are the ideas and attitudes that Object People cluster around:<\/p>\n<ul>\n<li>Strong support for Object Oriented coding style. (I mean, <i>obviously<\/i>.)<\/li>\n<li>The compiler is your friend.<\/li>\n<li>Maintainability and readability are everything. Well-written code should be <b>beautiful<\/b>.<\/li>\n<li>It&#8217;s all about the <b>code<\/b>.<\/li>\n<li>If you want to improve your work, you need to better understand design patterns, templates, and coding styles.<\/li>\n<\/ul>\n<p>While the Data People are more likely to favor these ideas.<\/p>\n<ul>\n<li>Strong support for the notion that there is no single coding style for all tasks. It all depends on the problem you&#8217;re trying to solve.<\/li>\n<li>You can&#8217;t blindly trust the compiler because it won&#8217;t always do what you expect.<\/li>\n<li>Performance and stability are everything. Well-written code should <b>work<\/b>.<\/li>\n<li>It&#8217;s all about the <b>data and the hardware<\/b>. Your goal is to manipulate the data with the hardware, not to separate the two with layers of abstractions.<\/li>\n<li>If you want to improve your work, you need a better understanding of the hardware and the data.<\/li>\n<\/ul>\n<p>Again, I want to stress that these things aren&#8217;t always mutually exclusive. Data People want beautiful code and Object People want code that works. I&#8217;m taking this noisy, scattered gradient between these two extreme positions shoving everyone to one end of the spectrum or the other. I&#8217;m well aware that you can&#8217;t sort all programmers into two buckets like this. However, in the interest of not cluttering this up with additional digressions and qualifying asterisks and teasing out all the nuances of this stuff, I&#8217;m going to pretend that we live in a two-bucket world.<\/p>\n<p>An Object Person might say something like:<\/p>\n<blockquote><p>Object Oriented design allows you to create code that maps to real-world structures and relationships. Your program can have a SpaceMarine object, and everyone reading the code will instantly know what that is. The SpaceMarine can hold an object called Gun, and the Gun object can have properties that can be immediately understood.<\/p>\n<p>This design provides a double benefit. One, it allows the designer to express solutions in a way that\u00a0 matches the problem they&#8217;re trying to solve. Two, it allows anyone maintaining the code later to understand the structure of the program based on the objects it uses. Another coder can come in and intuit the existence and purpose of SpaceMarine-&gt;Gun-&gt;Shoot () without needing to painstakingly scroll through all the code.<\/p><\/blockquote>\n<p>To which the Data Person might reply:<\/p>\n<blockquote><p>The metaphor suggested by your class structure is a lie, and it keeps you from reasoning about what the program is doing. Your SpaceMarine isn&#8217;t actually carrying a Gun because in terms of data, there&#8217;s no such thing. Your &#8220;gun&#8221; is a combination of three completely different data structures:<\/p>\n<ol>\n<li>The shared gun model that gets rendered. There&#8217;s only one copy of this and it never changes.<\/li>\n<li>The collection of behavioral characteristics that define a particular type of firearm: Reload speed, fire rate, kick, accuracy, etc. These attributes might change from one type of gun to the next, but all SpaceMarines carrying the same type of gun will share a common copy of this data.<\/li>\n<li>A set of data describing what a particular SpaceMarine is doing with a particular gun. This includes what animation is playing, how many bullets are left, the attachment location at the SpaceMarine&#8217;s hand, etc.<\/li>\n<\/ol>\n<p>You&#8217;ve taken these three totally different concepts and mashed them together. Your code is promising that SpaceMarines carry guns like in the real world, but the reality is that SpaceMarines technically &#8220;share&#8221; a single gun. If another coder tries to modify the gun for a particular marine, they&#8217;ll be modifying the global gun used by everyone, so the code is actively misleading by pretending to work like the real world.<\/p>\n<p>You&#8217;ve mixed the gun location data in with gameplay data, so now the positional data needed for rendering isn&#8217;t packed together in memory, which means you don&#8217;t have a good way to render all the guns in a single batch. This stupid metaphor is costing processor cycles and preventing you from thinking about what the computer is actually doing when it runs your code.<\/p><\/blockquote>\n<p>The Object Person would counter that no, these things aren&#8217;t a problem if you do <b>proper<\/b> Object Oriented design and the system works fine if you know how to do it &#8220;right&#8221;.<\/p>\n<p>The Data Person will counter that regardless of what the hypothetical &#8220;right&#8221; way is, this confounding and useless way is how the system is taught.<\/p>\n<p>Then the Object Person will claim The Data Person clearly hasn&#8217;t ever bothered to learn the thing they&#8217;re trying to critique.\u00a0The discussion quickly spirals out of control from there. I think you get the idea.<\/p>\n<p>The best example of the Data camp is this talk by developer Mike Acton: <a href=\"https:\/\/www.youtube.com\/watch?v=rX0ItVEVjHc\">CppCon 2014: Mike Acton &#8220;Data-Oriented Design and C++&#8221;<\/a>. Acton is opinionated and brusque. By assuming that all programs need to push the hardware for optimal performance, he engages in a lot of the domain tunnel vision I talked about <a href=\"?p=47821\">at the start of this series<\/a>. Having said that, he makes a lot of important points about how being ignorant of the fine-grain details of your data and hardware can lead to huge performance penalties.<\/p>\n<p>I don&#8217;t think this is a battle between right and wrong. I think this is a battle over suitability. When you&#8217;re trying to transform megabytes of data<span class='snote' title='2'>In this case &#8220;transforming data&#8221; is just high falutin&#8217; coder talk for &#8220;make shit explode, die, fall over, reload, or otherwise change&#8221;.<\/span> as a result of interactions between thousands of objects from a dozen different chaotic systems and you have a very narrow window of time to work with, then you need to be very focused on your data. You need to think about what needs to be done to that data, how it&#8217;s arranged in memory, how long the transformation will take, and how you&#8217;ll present the result to the graphics hardware.<\/p>\n<p>On the other hand, not every program needs this fanatical attention lavished on trivial blocks of memory. The classic example is when you&#8217;re building a GUI interface. Slider, buttons, input boxes, and check boxes all lend themselves really well to an object-oriented approach. The program often spends most of its runtime idle, waiting for user input. In these cases, those layers of abstraction can take a lot of the burden off our poor programmer. They don&#8217;t need to fuss over data structures or police compiler output to make sure it didn&#8217;t do anything destructive. If raw performance isn&#8217;t your top priority, then you can focus on making pretty, comfortable, easy-to-maintain, self-documenting code.<\/p>\n<h3>Games Programming is Kind of Both<\/h3>\n<p>The thing is, a video game exists in <b>both of these realms<\/b> simultaneously. Sometimes you&#8217;re talking to the graphics hardware and you need direct memory access and a complete understanding of what will happen when the code is run. Sometimes you need to pick a random number for bonus headshot damage and you don&#8217;t care if you waste a couple of processor cycles on needless precision.<\/p>\n<p>If I&#8217;m dealing with the graphics hardware or manipulating thousands of particles, then I want to get close to the metal and write something that looks a lot like raw old-school C. On the other hand if I was handling interface stuff or gameplay mechanics, then I&#8217;d want to express myself in a comfortable language like C# where I can leave all the heavy lifting to the compiler.<\/p>\n<h3>Vexation #5: Adhering to Dogmatic Design Principles is not Programming.<\/h3>\n<p><a href='https:\/\/www.reddit.com\/r\/ProgrammerHumor\/comments\/ddkyea\/types_of_personalities\/'><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/vex_stupid_question.jpg' width=100% alt='Shamelessly swiped from Reddit. Click to see the thread. Unlike StackOverflow, some of the replies are pretty good.' title='Shamelessly swiped from Reddit. Click to see the thread. Unlike StackOverflow, some of the replies are pretty good.'\/><\/div><\/a><div class='mouseover-alt'>Shamelessly swiped from Reddit. Click to see the thread. Unlike StackOverflow, some of the replies are pretty good.<\/div><\/p>\n<p>Java is a good example of a &#8220;big idea&#8221; language. It&#8217;s specifically designed to support and enforce object oriented design patterns. You can&#8217;t just make some code to play a sound. Playing a sound is an action, and so you need that action to be owned by an object. Instead of PlaySound (), you create a SoundPlayer class, you create an instance of that class, and then you order it to do what you want in the form of SoundPlayer.Play ().<\/p>\n<p>This problem where actions are owned by objects leads to the somewhat absurd <a href=\"https:\/\/steve-yegge.blogspot.com\/2006\/03\/execution-in-kingdom-of-nouns.html\">Kingdom of the Nouns<\/a>. There&#8217;s nothing inherently wrong with this style of coding, and I&#8217;ve run into many situations where this style felt really good. But in Java, you&#8217;re <b>not allowed<\/b> to deviate from it. You&#8217;re forced to use object oriented design, even if that design <a href=\"https:\/\/medium.com\/@cscalfani\/goodbye-object-oriented-programming-a59cda4c0e53\">doesn&#8217;t suit your project<\/a>. You have to figure out how to express this verb \/ action using a noun \/ object design. You end up writing containers for actions and creating all these extra classes<span class='snote' title='3'>Not to mention the source files to hold them.<\/span> and bloating code just to adhere to object-oriented orthodoxy.<\/p>\n<p>Likewise, Haskel is a &#8220;big idea&#8221; language. It&#8217;s built around the idea of <a href=\"https:\/\/en.m.wikipedia.org\/wiki\/Functional_programming\">functional programming<\/a>. Functional programming is designed to solve the problem of cascading state changes that I talked about <a href=\"?p=47906\">back in part 2<\/a>. Consider a line of code like this:<\/p>\n<pre lang=\"cpp\">Player.Gun.Fire (aim_location);<\/pre>\n<p>That seems trivial enough. But then you run the program and encounter a situation where shooting the gun created a bullet that blew up a red barrel that created a spherical damage zone that generated particles and sounds and damaged a support pillar that triggered a physics event. That one line of code makes massive changes to the state of the world. These changes are varied, numerous, and extensive<span class='snote' title='4'>In the real world, you&#8217;d have some sort of event-based system that would wait until the gun was done being fired before kicking off any of those other systems. But explaining how that works would take a lot of page space, and this example is good enough for illustrating the problems of expanding state change.<\/span>. <\/p>\n<p>Functional programming doesn&#8217;t allow objects in the program to make changes to each other. A bullet can&#8217;t damage a bad guy. Instead, there needs to be some sort of master object that owns all the bullets and bad guys. When a collision happens, the owner says to the bad guy, &#8220;Here&#8217;s a bullet. Shoot yourself with it and return a version of yourself exhibiting the result.&#8221; So then the owner throws away the old version of the bad guy and replaces it with the version that&#8217;s been shot.<\/p>\n<p>This means you&#8217;ll never have any surprise side-effects and things will theoretically be easier to debug. However, this style of programming is very tricky and has a large performance overhead<span class='snote' title='5'>I imagine the need to create a &#8220;new&#8221; SpaceMarine every frame to replace the one from last frame means there will be a lot of churn in memory allocations. But I&#8217;ve never done this sort of coding so I can&#8217;t say for sure.<\/span>.<\/p>\n<p>I want to stress that <strong>Java and Haskell are not bad languages because of these restrictions<\/strong>. These languages were designed around these ideas <strong>on purpose<\/strong>, and faulting the languages for sticking to their core design is somewhat missing the point. It&#8217;s like complaining that the low blade clearance on a band saw makes it tough to cut bread. There&#8217;s nothing wrong with the tool, it&#8217;s just not the tool you should be using for this job.<\/p>\n<p>(Of course, then you come to the problem where your workplace forces you to use the wrong tool. That sucks, but that&#8217;s a problem with people and not the language.)<\/p>\n<p>Having said all that, strict object-oriented design is a bad fit for games<span class='snote' title='6'>Although informal OO design is fine. Objects are fine, as long as you&#8217;re allowed to deviate from OO when doing low-level stuff.<\/span>, and functional programming even moreso. I know I&#8217;ve spent a lot of time in this series fantasizing about what a modernized language for GameDev might look like, but the limitations of hardware mean we&#8217;re still bound by the Old Ways. We&#8217;re still going to need occasional direct memory access and unguarded access to memory. A GameDev language needs to be forward-looking, but it also needs to hang onto the 1972 way of doing things.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a bit of a schism in the world of programming. This divide isn&#8217;t over a single issue, but instead over a sort of emerging design philosophy that tends to cluster around particular ideas. It&#8217;s complex and multifaceted, and you could spend an entire book exploring all the various differences. In the broad strokes people [&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-48554","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\/48554","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=48554"}],"version-history":[{"count":17,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/48554\/revisions"}],"predecessor-version":[{"id":48571,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/48554\/revisions\/48571"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}