Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What does OOP stand for?


Published on

What does OOP stand for?

When Object Oriented Programming(OOP) is taught so extensively, do computer programmers, specifically within games development, realise what it's possibly doing to productivity and performance? I explain my own view from experience in personal projects and professional work.

This talk was given to the Edinburgh meet of IGDA Scotland, on 2011/07/27.

Published in: Technology, Design
  • Login to see the comments

What does OOP stand for?

  1. 1. Object Oriented Programming What does OOP stand for? Colin Riley @domipheus
  2. 2. Me <ul><li>Been at Codeplay five years </li></ul><ul><li>Most of that time working with PS3 </li></ul><ul><li>Recently looking into GPU stuff, OpenCL, etc. </li></ul><ul><li>Got a little hobby game I’ve been working on for several years; using XNA; hope to get it onto XBLIG one day. </li></ul>
  3. 3. Coding talks <ul><li>Can get stale quickly. </li></ul><ul><li>Will try to keep it short. </li></ul><ul><li>General Q&A at the end. </li></ul><ul><li>If you are not a coder, I’m sorry  </li></ul>
  4. 4. To begin, a question. <ul><li>Lets have a show of hands; </li></ul><ul><ul><li>How many here consider themselves coders? </li></ul></ul><ul><ul><li>… . of those studied OOP at uni/school/etc? </li></ul></ul><ul><ul><li>… . how many were told OOP is the best way of doing things? </li></ul></ul><ul><li>Time to get controversial. </li></ul>
  5. 5. My view <ul><li>Far too much emphasis on OOP, everywhere. </li></ul><ul><li>Massive in games dev – why? </li></ul><ul><ul><li>Game objects == code objects? </li></ul></ul><ul><li>Why do I think OOP causes issues? </li></ul>
  6. 6. A quick link back <ul><li>At the last meet in Glasgow, Brian McDonald from Glasgow Caley talked. </li></ul><ul><ul><li>Days long game jam work was often more awesome than month(s) long project work. </li></ul></ul><ul><li>Is OOP to blame? </li></ul>
  7. 7. For me, it plays a part. <ul><li>With uni projects you have a fear to fail. </li></ul><ul><ul><li>Code must look and feel ‘good’ </li></ul></ul><ul><ul><li>OOP is how it’s done </li></ul></ul><ul><ul><li>OOP tends to make coders take things too far </li></ul></ul>
  8. 8. So what can OOP stand for? O OP O bfuscates P roductivity OOP
  9. 9. Ehh? What you smoking? <ul><li>OOP makes coders develop in a defined style instead of ‘getting stuff done’. </li></ul><ul><li>Some incredible understanding that OOP makes things simpler and easier to maintain. </li></ul><ul><li>That’s true isn’t it? </li></ul>
  10. 10. <ul><li>I mean, you've got to to have a main game object which creates an abstract factory of game systems , new's your engine object , which initializes all the subsystem singletons , the entity system creating our component system , in turn initializing our hash table of string referenceable game entity factories , which the main game object then queries depending on our XMLReader gamedata tree structure, setting proxies as required to then… </li></ul>
  11. 11. WTF
  12. 13. WTF
  13. 15. WTF
  14. 18. WTF
  15. 20. WTF
  16. 22. WTF
  17. 24. SOFTWARE IS A PLATFORM? <ul><li>OOP; Systems must be able to operate on any object; generics; </li></ul><ul><li>Whatever…. </li></ul><ul><li>Software is not a platform. Stop wasting time trying to develop for it. It’s text for crying out loud. </li></ul><ul><li>The hardware is the real platform </li></ul>See ‘Three Big Lies’ by Mike Acton
  18. 25. <ul><li>OOP forces programmers to develop the source code architecture, rather than the solution to a problem. </li></ul><ul><li>The computer doesn’t care that you have beautifully designed subsystems, all abstracted out and encapsulated like a pro. </li></ul><ul><li>In fact it probably hates you for it. We’ll come to that soon… </li></ul>
  19. 26. More examples I retweeted a talk that Jonathan Blow of Braid fame gave to Berkeleys compsci group. He mentioned that he now does pretty much everything in simple flat arrays, no complicated data structures.
  20. 27. Productivity <ul><li>When you are in a small team; encapsulation , design patterns , blah blah is just noise. What you want is to develop your game, and the quickest way of doing this is usually as Jonathan states - just chuck everything in a fixed sized array and linearly search over it. </li></ul>
  21. 28. LittleBigPlanet <ul><li>At a panel session in Develop this year, Media Molecule discussed one of their first demos. </li></ul><ul><li>Programmer was away, so folk with no experience did the programming. </li></ul><ul><li>No pointers. </li></ul><ul><li>Everything in global variables. </li></ul><ul><li>They said it was so easy to debug and follow. </li></ul>
  22. 29. Need a system for handling the states of your things?
  23. 30. That does not mean you need a massive component system that is scalable and can handle any matter of things and things v2.0 and things v3.0
  24. 31. <ul><li>Just throw it all in a fixed size array and loop over it. </li></ul><ul><li>Need another type of thing? Another array and another loop. </li></ul><ul><li>You didn’t need a system singleton with ThingFactory and TraverseThisWayTrait. </li></ul><ul><li>No. You don’t. </li></ul>
  25. 32. But what about the future? <ul><li>Well if you realise you do need some overcomplicated system in 6 months time… </li></ul><ul><li>You look at the code, see a fixed array, see a for loop, and it’s instantly obvious what’s happening. </li></ul><ul><li>Easy to then swap out the systems. </li></ul>
  26. 33. So… <ul><li>When you need to get something working quickly in the code… </li></ul><ul><li>Would you rather make the code look all cool and hip… </li></ul><ul><li>Or just Get It Working™ </li></ul><ul><li>OOP Obfuscates Productivity by distracting coders from developing solutions to problems. </li></ul>
  27. 34. Hrm… <ul><li>Yes, OOP can make sense in places. Massive teams with different skillsets that need distinct code and data blocks that everyone can work Independently on… </li></ul><ul><li>But in game development, we need high performance. </li></ul><ul><li>So what does OOP do for that? </li></ul>
  28. 35. The beginning. <ul><li>OOP = objects </li></ul><ul><ul><li>Objects have Code </li></ul></ul><ul><ul><li>Objects have Data </li></ul></ul>
  29. 39. Why is that? <ul><li>How can OOP be a performance drain? </li></ul>
  30. 45. How do we get the data up to the code?
  31. 49. And here where OOP falls flat <ul><li>To get performance out of consoles, you need to operate on multiple data. </li></ul><ul><li>OOP, at its heart, is single code to single data. </li></ul>
  32. 50. So what can OOP stand for? O OP O bliterates P erformance OOP (in high data throughput cases, which there are many in games) is basically
  33. 51. An example <ul><li>C++ </li></ul><ul><li>We have a collection of ‘things’ </li></ul><ul><li>We need to update positions each frame </li></ul><ul><li>Sometimes we need to update state, not always. </li></ul><ul><li>LETS CODE! </li></ul>
  34. 52. struct Details { int numShots; int numDeaths; int numTargets; int lifetime; int state; int priority; int health; int ammo; int friendThings[4]; }; // 48 bytes class Thing { Vector3 mPosition; Quat mOrientation; Colour mColour; Vector3 mVelocity; float mDecay; Details mDetails; }; // 96 bytes
  35. 53. OOP ish class ThingSystem { String mName; std::vector<Thing> mThings; .... }; void Thing :: UpdatePosition () { mPosition += mVelocity; mVelocity *= mDecay; } void ThingSystem :: UpdateThingPositions () { for( vector <int>:: iterator iter = mThings. begin (); iter != mThings. end (); iter++) { *iter. UpdatePosition (); } }
  36. 54. Now… <ul><li>CPU’s fetch data from memory </li></ul><ul><li>Data from memory goes to a local cache which is very close to the CPU so it’s quick to access. </li></ul><ul><li>You want stuff to fit in your cache. </li></ul><ul><li>This is a simplified view, but it’s just as valid. </li></ul><ul><li>Lets say we have a small 512byte cache split into 64byte chunks/lines. </li></ul>
  37. 55. OOP fits 4 things.
  38. 57. Not great is it?
  39. 58. Enter DOD <ul><li>Data Oriented Design </li></ul><ul><li>Quoting @noel_llopis </li></ul><ul><li>“ Data-oriented design shifts the perspective of programming from objects to the data itself: The type of the data, how it is laid out in memory, and how it will be read and processed in the game. “ </li></ul>
  40. 59. Enter DOD <ul><li>We know from the diagrams earlier that stacking up your data can make it reach good performance on the CPU. </li></ul><ul><li>Organise out the data differently… </li></ul>
  41. 60. DOD ish class ThingSystem { Vector3 mThingPosition[MAX_THINGS]; Quat mThingOrientation[MAX_THINGS]; Colour mThingColour[MAX_THINGS]; Vector3 mThingVelocity[MAX_THINGS]; float mThingDecay[MAX_THINGS]; Details mThingDetails[MAX_THINGS]; int mNumThings; .... }; void ThingSystem :: UpdateThingPositions () { for(int i = 0; i< mNumThings; i++) { mThingPosition[i] += mThingVelocity[i]; mThingVelocity[i] *= mThingDecay[i]; } }
  42. 61. Doesn’t look that different? <ul><li>We still have this manager thing. </li></ul><ul><li>We’re still updating positions every frame as we require. </li></ul><ul><li>We can still get access to a single thing to do the rare update cases via the index to the arrays… </li></ul><ul><li>What difference does it make? </li></ul>
  43. 62. DOD fits 16 things.
  44. 64. So what keeps the OOP version lower on the performance scale?
  45. 65. Costs <ul><li>Lets say that inner code block of the loop takes 30 cycles to complete. </li></ul><ul><li>Lets also assume that fetching the next 512byte chunks of cache takes 500 cycles. </li></ul><ul><li>(again, simplified for ease of discussion) </li></ul>
  46. 67. Lets put that into numbers <ul><li>If we had 200 things… </li></ul><ul><li>OOP </li></ul><ul><ul><li>Would take 25,000 cycles to Update </li></ul></ul><ul><li>DOD </li></ul><ul><ul><li>Would take 6,500 cycles to Update </li></ul></ul>
  47. 68. Lets think a minute. <ul><li>OOP wants a single object </li></ul><ul><ul><li>But we always update all of them per frame… </li></ul></ul><ul><ul><li>Only rarely do we want a single object </li></ul></ul><ul><ul><ul><li>Maybe it dies or something and we mark it as such at the end of the game update step. </li></ul></ul></ul><ul><li>So why the hell are we using OOP!?!? </li></ul><ul><ul><li>Ask yourself that question. </li></ul></ul><ul><li>OOP Obliterates Performance by being at odds with the underlying hardware. </li></ul>
  48. 69. Thinking again… <ul><li>That DOD example is basically just the representation of the OOP code using simple fixed-sized arrays of simple types… </li></ul><ul><li>Didn’t we mention that at the start of the talk? </li></ul>
  49. 70. More thinking… <ul><li>Ohh yes. </li></ul><ul><li>That’s what we did to ‘get stuff done’ when we thought no one was looking at our super-ugly non OOP code… </li></ul><ul><li>So in fact; just getting stuff done may be the fastest way of doing it? </li></ul>
  50. 71. EVEN MORE THINKING <ul><li>surely iterating over whole arrays has got to be slower in the big picture than having a big structure which tells you which to update at a given time. Otherwise it’s brute force right? </li></ul><ul><li>Are you sure about that? </li></ul><ul><li>Now that’s something to think about. </li></ul>
  51. 72. For a real world example… <ul><li>Culling the Battlefield: Data Oriented Design in Practice. </li></ul><ul><li>Daniel Collin (DICE) </li></ul><ul><li> </li></ul>
  52. 73. In conclusion <ul><li>OOP can hinder productivity by making programmers take things too far instead of solving the problems first and foremost. </li></ul><ul><li>OOP had a direct impact on performance as the main idea around it is at odds with modern computer hardware </li></ul><ul><li>Programmers need to think more about the problem and choose what is right for them. </li></ul>
  53. 74. Well, thanks. <ul><li>I hope this gave the coders out there something to think about. </li></ul><ul><li>OOP has its place, it’s just far too widespread now and the cracks are showing (and have been for ages) </li></ul><ul><li>If you are not a coder, again I’m sorry  </li></ul>
  54. 75. Follow me, @domipheus, on Twitter! <ul><li>Great source of stuff on DOD here, has links to articles, etc: </li></ul><ul><li> </li></ul><ul><li>Direct quotes and inspiration from: </li></ul><ul><ul><li>Three Big Lies , @mike_acton </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Pitfalls of Object Oriented Programming , @TonyAlbrecht </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Data-Oriented Design (Or Why You Might Be Shooting Yourself in The Foot With OOP) , @noel_llopis </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Introduction to Data Oriented Design, @daniel_collin </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Programming Aesthetics learned from making independent games, @jonathan_blow </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Student Projects vs Game Jams , Brian McDonald, @BigBearScot </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul>Questions?
  55. 76. Why twitter is awesome!
  56. 77. Twitter == awesome
  57. 78. Twitter == awesome
  58. 79. Twitter == awesome
  59. 80. Twitter == awesome <ul><li>Maybe you should start a new hashtag </li></ul><ul><li>#oopstandsfor ? </li></ul><ul><li>#oopstandsfor Out of Order programming </li></ul><ul><li>#oopstandsfor Obscure Over-engineered Problems </li></ul><ul><li>And so on…  </li></ul>