Upgrade to Pro — share decks privately, control downloads, hide ads and more …

We are all C++ teachers

We are all C++ teachers

Explaining an idea is hard. This difficulty grows exponentially the more people you need to talk to. This talk will focus on my journey in learning C++ and then teaching it to others. Touching on things like being a student at University, opening my Twitter DM's to the world, showing common pitfalls in the language and how we can approach others when we want to explain a complex subject.

Ólafur Waage, a native Icelander that has lived in Sweden for the past 4 years. A Senior Programmer at Ubisoft Massive on the Uplay PC Backend Team. Before he was a Developer in the Air Traffic Control industry. Lover of games, music and puzzles.

Ólafur Waage

August 23, 2018
Tweet

More Decks by Ólafur Waage

Other Decks in Programming

Transcript

  1. Who is "The Icelander" Ólafur Waage Malmö for the last

    4 years Senior Programmer at Ubiso Massive 5 / 248
  2. Who is "The Icelander" Ólafur Waage Malmö for the last

    4 years Senior Programmer at Ubiso Massive Backend Team on the Uplay PC Project 6 / 248
  3. Who is "The Icelander" Ólafur Waage Malmö for the last

    4 years Senior Programmer at Ubiso Massive Backend Team on the Uplay PC Project Worked in the ATC industry before this 7 / 248
  4. This talk The structure of this talk is 4 stories

    about learning They're different from another but all focus on learning and teaching in some manner 9 / 248
  5. Let's talk about learning First, some preamble Nobody learns in

    the same manner This is not a black and white situation, this is a multidimensional problem 12 / 248
  6. Let's talk about learning First, some preamble Nobody learns in

    the same manner This is not a black and white situation, this is a multidimensional problem These kinds of categorizations are contested but they help the points I'm making 13 / 248
  7. Let's talk about learning First, some preamble Nobody learns in

    the same manner This is not a black and white situation, this is a multidimensional problem These kinds of categorizations are contested but they help the points I'm making Many grains of salt 14 / 248
  8. Visual Learner Abstract Images Scribbling figures into a notebook Drawing

    diagrams and how they relate to other diagrams 20 / 248
  9. Auditory Learner Learn by listening Asking questions for clarification Group

    discussions Use of mnemonic devices (Please excuse my dear aunt sally) 21 / 248
  10. Read / Write Writing notes, lots of notes Books, Reports,

    Essays, all great sources of knowledge Translate abstract ideas into text 22 / 248
  11. Back to school First semester of University, about halfway through

    Programming 101 We're doing C++ (yay) 27 / 248
  12. Back to school First semester of University, about halfway through

    Programming 101 We're doing C++ (yay) This picture is placed on the projector 28 / 248
  13. Pointers We had a two hour class That was nothing

    but these kinds of pictures with code on the side 32 / 248
  14. Pointers We had a two hour class That was nothing

    but these kinds of pictures with code on the side Stepping through each use case for pointers 33 / 248
  15. Pointers We had a two hour class That was nothing

    but these kinds of pictures with code on the side Stepping through each use case for pointers 195 slides 34 / 248
  16. Pointers++ Speaker was fine Going through the talk now, there's

    nothing wrong in the talk itself (informationally) 37 / 248
  17. Pointers++ Speaker was fine Going through the talk now, there's

    nothing wrong in the talk itself (informationally) But I was completely lost 38 / 248
  18. Pointers++ Speaker was fine Going through the talk now, there's

    nothing wrong in the talk itself (informationally) But I was completely lost I knew what pointers were for 39 / 248
  19. Pointers++ Speaker was fine Going through the talk now, there's

    nothing wrong in the talk itself (informationally) But I was completely lost I knew what pointers were for I knew how to use them 40 / 248
  20. Pointers++ Speaker was fine Going through the talk now, there's

    nothing wrong in the talk itself (informationally) But I was completely lost I knew what pointers were for I knew how to use them This talk was great for Auditory and Visual learners 41 / 248
  21. *Pointers Any pointer exercise made me scared There was always

    this doubt it my mind, that I was missing something 43 / 248
  22. *Pointers Any pointer exercise made me scared There was always

    this doubt it my mind, that I was missing something I knew pointers were in C as well, and back then I had a "very legal" PDF of "The C Programming Language" 44 / 248
  23. *Pointers Any pointer exercise made me scared There was always

    this doubt it my mind, that I was missing something I knew pointers were in C as well, and back then I had a "very legal" PDF of "The C Programming Language" Let's figure this out, what does the book say 45 / 248
  24. *Pointers Any pointer exercise made me scared There was always

    this doubt it my mind, that I was missing something I knew pointers were in C as well, and back then I had a "very legal" PDF of "The C Programming Language" Let's figure this out, what does the book say (PS. These screenshots I got from amazon and similar places giving free samples of the book) 46 / 248
  25. *Pointers++ Page 93 ??? Chapter 5 ??? How long is

    this book? It's 185 pages (a bit more with appendix and a reference manual) 54 / 248
  26. *Pointers++ Page 93 ??? Chapter 5 ??? How long is

    this book? It's 185 pages (a bit more with appendix and a reference manual) 50.27% through the book 55 / 248
  27. *Pointers++ Page 93 ??? Chapter 5 ??? How long is

    this book? It's 185 pages (a bit more with appendix and a reference manual) 50.27% through the book I went to Page 93 56 / 248
  28. *Pointers++ Page 93 ??? Chapter 5 ??? How long is

    this book? It's 185 pages (a bit more with appendix and a reference manual) 50.27% through the book I went to Page 93 I read the first sentence and then I put the book down 57 / 248
  29. *Pointers++ Page 93 ??? Chapter 5 ??? How long is

    this book? It's 185 pages (a bit more with appendix and a reference manual) 50.27% through the book I went to Page 93 I read the first sentence and then I put the book down It was exactly what I was missing. Exactly. 58 / 248
  30. &Pointers Variables store numbers, this I knew If someone would

    ask me, "what is an address?", I would say "It's just a number" 61 / 248
  31. &Pointers Variables store numbers, this I knew If someone would

    ask me, "what is an address?", I would say "It's just a number" But I never made the connection, because I was fixed on that hammered in box arrow box mental model 62 / 248
  32. &Pointers Variables store numbers, this I knew If someone would

    ask me, "what is an address?", I would say "It's just a number" But I never made the connection, because I was fixed on that hammered in box arrow box mental model It's not magic 63 / 248
  33. *&Pointers Mental models are very important Especially when you are

    starting out learning a topic They can be the springboard you need to learn more 67 / 248
  34. *&Pointers Mental models are very important Especially when you are

    starting out learning a topic They can be the springboard you need to learn more But they can also harm if vital aspects are missing 68 / 248
  35. Quick Example Who here knows what a Monad is? Monad

    is a monoid in the category of endofunctors, duh! 71 / 248
  36. Quick Example Who here knows what a Monad is? Monad

    is a monoid in the category of endofunctors, duh! This is not helping anyone 72 / 248
  37. Quick Example Who here knows what a Monad is? Monad

    is a monoid in the category of endofunctors, duh! This is not helping anyone ...well maybe some, but not me 73 / 248
  38. Quick Example Who here knows what a Monad is? Monad

    is a monoid in the category of endofunctors, duh! This is not helping anyone ...well maybe some, but not me Here's an example of a mental model. Is it perfect? No. But it helped me get started. 74 / 248
  39. A foot in the door You have a huge library

    of stand alone functions that look something like this 75 / 248
  40. A foot in the door You have a huge library

    of stand alone functions that look something like this ErrorCode IModifyTheIncomingValue( IAmSomeComplexDataType& aCDT) { if(aCDT.WeDoSomeChecks()) { return SomeErrorCode; } // Modify aCDT in some way and return ErrorCode_OK } 76 / 248
  41. Oh no the foot is stuck But it's such a

    hassle to call these functions 77 / 248
  42. Oh no the foot is stuck But it's such a

    hassle to call these functions WhoKnows LetsCallOneOfTheseBadBoys() { IAmSomeComplexDataType cdt; const auto errorCode = IModifyTheIncomingValue(cdt); if(errorCode == SomethingBad) { // there might be many of these if checks return SomeThing; } // Here it's ok to use cdt } 78 / 248
  43. Please help, it's getting numb What if you want to

    chain many of these modifying functions 79 / 248
  44. Please help, it's getting numb What if you want to

    chain many of these modifying functions And only call the next function if the previous one was successful 80 / 248
  45. Please help, it's getting numb What if you want to

    chain many of these modifying functions And only call the next function if the previous one was successful const auto errorCode = IModifyTheIncomingValue(cdt); if(errorCode == SomethingBad) { return SomeThing; } const auto someOtherErrorCode = IAlsoModify(cdt); if(someOtherErrorCode == OhNo) { return SomeOtherThing; } // etc etc 81 / 248
  46. Ahh thanks What if there was a language feature where

    you could chain together modifying functions like that 82 / 248
  47. Ahh thanks What if there was a language feature where

    you could chain together modifying functions like that And the language would do the "exception handling" automatically for you 83 / 248
  48. Ahh thanks What if there was a language feature where

    you could chain together modifying functions like that And the language would do the "exception handling" automatically for you return cdt -> ModifyOne() -> ModifyTwo() -> ICanKeepModifying(); 84 / 248
  49. Ahh thanks What if there was a language feature where

    you could chain together modifying functions like that And the language would do the "exception handling" automatically for you return cdt -> ModifyOne() -> ModifyTwo() -> ICanKeepModifying(); Is this a perfect mental model? No, far from it, the whole field is immense, but it was enough for me to dive deeper 85 / 248
  50. Ahh thanks What if there was a language feature where

    you could chain together modifying functions like that And the language would do the "exception handling" automatically for you return cdt -> ModifyOne() -> ModifyTwo() -> ICanKeepModifying(); Is this a perfect mental model? No, far from it, the whole field is immense, but it was enough for me to dive deeper And mental models should evolve, I'll mention more on that later 86 / 248
  51. Back to the future In November of 2017 I decided

    to open up my Twitter DM's 88 / 248
  52. Back to the future In November of 2017 I decided

    to open up my Twitter DM's The focus was to have a 1 on 1 code review with people new to programming 89 / 248
  53. Code Reviews - Overview I have done over 50 reviews

    I have denied about the same amount, because they are not beginners and/or reviewing their code would take hours 92 / 248
  54. Code Reviews - Overview I have done over 50 reviews

    I have denied about the same amount, because they are not beginners and/or reviewing their code would take hours Most contacting me are not asking for code reviews 93 / 248
  55. Code Reviews - Overview I have done over 50 reviews

    I have denied about the same amount, because they are not beginners and/or reviewing their code would take hours Most contacting me are not asking for code reviews But are asking for advice regarding job hunting 94 / 248
  56. Code Reviews - Overview I have done over 50 reviews

    I have denied about the same amount, because they are not beginners and/or reviewing their code would take hours Most contacting me are not asking for code reviews But are asking for advice regarding job hunting Where to learn C++ 95 / 248
  57. Code Reviews - Overview I have done over 50 reviews

    I have denied about the same amount, because they are not beginners and/or reviewing their code would take hours Most contacting me are not asking for code reviews But are asking for advice regarding job hunting Where to learn C++ If their CV is ok 96 / 248
  58. Code Reviews - The Bad Having an open DM can

    be a negative thing 101 / 248
  59. Code Reviews - The Bad Having an open DM can

    be a negative thing I got sent a lot of graphic photos 102 / 248
  60. Code Reviews - The Bad Having an open DM can

    be a negative thing I got sent a lot of graphic photos I got sent a few shady URL's claiming to be where their code is 103 / 248
  61. Code Reviews - The Bad Having an open DM can

    be a negative thing I got sent a lot of graphic photos I got sent a few shady URL's claiming to be where their code is I was asked once to do a full security audit of a large application and to be marked responsible for it's validity 104 / 248
  62. Code Reviews - The Bad Having an open DM can

    be a negative thing I got sent a lot of graphic photos I got sent a few shady URL's claiming to be where their code is I was asked once to do a full security audit of a large application and to be marked responsible for it's validity But all of these are very much a minority of contacts, but they do happen 105 / 248
  63. Code Reviews - The Good People are so nice and

    respectful So many are just happy to have someone to chat with about their work 107 / 248
  64. Code Reviews - The Good People are so nice and

    respectful So many are just happy to have someone to chat with about their work I've done a few person to person skype chats with people 108 / 248
  65. Code Reviews - The Good People are so nice and

    respectful So many are just happy to have someone to chat with about their work I've done a few person to person skype chats with people I even did a 2 hour skype session with an Indian University, they were full of questions and so nice. (@NextTechLab) 109 / 248
  66. Code Reviews - The Summary Things have cooled down recently

    So I took all the code I have reviewed and did a summary of the most common things I point out 111 / 248
  67. Code Reviews - The Summary Things have cooled down recently

    So I took all the code I have reviewed and did a summary of the most common things I point out This is in no particular order 112 / 248
  68. One Function to Rule Them All void IDoEverything_IEvenModifyGlobalState() { //

    Pages of code } It seems that some are taught that they should create functions only when code is reused 114 / 248
  69. One Function to Rule Them All void IDoEverything_IEvenModifyGlobalState() { //

    Pages of code } It seems that some are taught that they should create functions only when code is reused Which tends to turn into the IDoEverything function 115 / 248
  70. One Function to Rule Them All void IDoEverything_IEvenModifyGlobalState() { //

    Pages of code } It seems that some are taught that they should create functions only when code is reused Which tends to turn into the IDoEverything function We generally have a good chat about that creating functions for 1 use is ok 116 / 248
  71. One Function to Rule Them All void IDoEverything_IEvenModifyGlobalState() { //

    Pages of code } It seems that some are taught that they should create functions only when code is reused Which tends to turn into the IDoEverything function We generally have a good chat about that creating functions for 1 use is ok Even similar bits of code can be turned into functions when generalized enough 117 / 248
  72. One Class to Rule Them All class IContainAllTheCode { //

    Visual Studio sometimes crashes when I'm opened } 118 / 248
  73. One Class to Rule Them All class IContainAllTheCode { //

    Visual Studio sometimes crashes when I'm opened } Multiple functions, sure but now we have all the state in one place 119 / 248
  74. One Class to Rule Them All class IContainAllTheCode { //

    Visual Studio sometimes crashes when I'm opened } Multiple functions, sure but now we have all the state in one place I feel this might be insecurity about how to abstract and how to access data from different locations 120 / 248
  75. The Pyramid of Death int AbandonHopeAllYeWhoEnterHere( EnumOfSomeState aWhyAmIBeingPassedIn, std::vector<Thing> aIAmPassedByValue)

    { if(aWhyAmIBeingPassedIn != EnumOfSomeState_Invalid) { if(!aIAmPassedByValue.empty()) { if(aWhyAmIBeingPassedIn == EnumOfSomeState_Small) { for(int i = 0; i < aIAmPassedByValue.size(); ++i) { // trimmed } } } } // More stuff below } 121 / 248
  76. The Pyramid flattened int MoreHopeThisTime( const EnumOfSomeState aIShouldNotChange, const std::vector<Thing>&

    aIAmNotCopied) { if(aIShouldNotChange == EnumOfSomeState_Invalid || aIAmNotCopied.empty()) { return 0; } // etc } 122 / 248
  77. The Pyramid I feel this comes with insecurity with &&

    and || operators The pyramid structure also is more of a natural structure, if you talk about your code 124 / 248
  78. The Pyramid I feel this comes with insecurity with &&

    and || operators The pyramid structure also is more of a natural structure, if you talk about your code "To loop through this list, we can't be in the invalid state, the list can't be empty and we do this loop only if we are in the small state" 125 / 248
  79. The Pyramid I feel this comes with insecurity with &&

    and || operators The pyramid structure also is more of a natural structure, if you talk about your code "To loop through this list, we can't be in the invalid state, the list can't be empty and we do this loop only if we are in the small state" Rater than 126 / 248
  80. The Pyramid I feel this comes with insecurity with &&

    and || operators The pyramid structure also is more of a natural structure, if you talk about your code "To loop through this list, we can't be in the invalid state, the list can't be empty and we do this loop only if we are in the small state" Rater than "If you are invalid or the list is empty, we will exit the function" 127 / 248
  81. Is magic real? int Magic( int aSomeValue, int aSomeOtherValue) {

    if(aSomeValue > 5) // magic number { // stuff } // some other stuff } 128 / 248
  82. WikiWikiWeb Zero One Infinity Rule - "Simply stated: You will

    either need zero of a thing, one of a thing, or an arbitrary number of the thing." http://wiki.c2.com/?ZeroOneInfinityRule 129 / 248
  83. WikiWikiWeb Zero One Infinity Rule - "Simply stated: You will

    either need zero of a thing, one of a thing, or an arbitrary number of the thing." http://wiki.c2.com/?ZeroOneInfinityRule We know that checking against 0 is common place and usually doesn't need context 130 / 248
  84. WikiWikiWeb Zero One Infinity Rule - "Simply stated: You will

    either need zero of a thing, one of a thing, or an arbitrary number of the thing." http://wiki.c2.com/?ZeroOneInfinityRule We know that checking against 0 is common place and usually doesn't need context Similar to 1 and in some cases 2 as well 131 / 248
  85. WikiWikiWeb Zero One Infinity Rule - "Simply stated: You will

    either need zero of a thing, one of a thing, or an arbitrary number of the thing." http://wiki.c2.com/?ZeroOneInfinityRule We know that checking against 0 is common place and usually doesn't need context Similar to 1 and in some cases 2 as well Any other number is magic and needs defined context, or at least a name 132 / 248
  86. WikiWikiWeb Zero One Infinity Rule - "Simply stated: You will

    either need zero of a thing, one of a thing, or an arbitrary number of the thing." http://wiki.c2.com/?ZeroOneInfinityRule We know that checking against 0 is common place and usually doesn't need context Similar to 1 and in some cases 2 as well Any other number is magic and needs defined context, or at least a name Is 5 the number of threads, seconds until next retry or the maximum number of users in a group? 133 / 248
  87. Inconsistent Formatting void imCalledCamelCase() { } void And_Im_Not_Nice_To_Type() { }

    This is a common thing with people who are very new to programming 135 / 248
  88. Inconsistent Formatting void imCalledCamelCase() { } void And_Im_Not_Nice_To_Type( ) {

    } This is a common thing with people who are very new to programming I tell them, what formatting style you use does not matter 136 / 248
  89. Inconsistent Formatting void imCalledCamelCase() { } void And_Im_Not_Nice_To_Type () {

    } This is a common thing with people who are very new to programming I tell them, what formatting style you use does not matter The compiler couldn't care less about how you format 137 / 248
  90. Inconsistent Formatting void im_Called_CamelC_ase() { } void And_Im_Not__Nice_To_Type() { }

    This is a common thing with people who are very new to programming I tell them, what formatting style you use does not matter The compiler couldn't care less about how you format It's going to parse it, errors and all 138 / 248
  91. Inconsistent Formatting void imCalledCamelCase() { } void And_Im_Not_Nice_To_Type ( )

    { } This is a common thing with people who are very new to programming I tell them, what formatting style you use does not matter The compiler couldn't care less about how you format It's going to parse it, errors and all Just be consistent 139 / 248
  92. // This is the title of the slide Comments everywhere

    // Here below Olafur will go over the problems of comments // This is the first point 142 / 248
  93. // This is the title of the slide Comments everywhere

    // Here below Olafur will go over the problems of comments // This is the first point So many comments, everywhere, written in all sorts of styles 143 / 248
  94. // This is the title of the slide Comments everywhere

    // Here below Olafur will go over the problems of comments // This is the first point So many comments, everywhere, written in all sorts of styles // Here is the third point 144 / 248
  95. // This is the title of the slide Comments everywhere

    // Here below Olafur will go over the problems of comments // This is the first point So many comments, everywhere, written in all sorts of styles // Here is the third point Comments can be helpful, but what happens is that you double the state of your code 145 / 248
  96. More comments A big problem with comments is that there

    is nothing connecting comments to the code 146 / 248
  97. More comments A big problem with comments is that there

    is nothing connecting comments to the code int ImportantFunction( int aValueOne, int aValueTwo) { // We don't need to check for <some problem> // because that's always done by the caller } 147 / 248
  98. More comments A big problem with comments is that there

    is nothing connecting comments to the code int ImportantFunction( int aValueOne, int aValueTwo) { // We don't need to check for <some problem> // because that's always done by the caller } But what if it's not? 148 / 248
  99. More comments A big problem with comments is that there

    is nothing connecting comments to the code int ImportantFunction( int aValueOne, int aValueTwo) { // We don't need to check for <some problem> // because that's always done by the caller } But what if it's not? A line of comment is as important as a line of code. Review appropriately. 149 / 248
  100. Crowded question Similar problem to one function to rule them

    all, a single if statement packed with checks 150 / 248
  101. Crowded question Similar problem to one function to rule them

    all, a single if statement packed with checks if(myFoo->IsThing(aBar + aBaz->GetStuff(18))) {} 151 / 248
  102. Crowded question Similar problem to one function to rule them

    all, a single if statement packed with checks if(myFoo->IsThing(aBar + aBaz->GetStuff(18))) {} If you're blocking execution with && it is understandable to pack if statements with things 152 / 248
  103. Crowded question Similar problem to one function to rule them

    all, a single if statement packed with checks if(myFoo->IsThing(aBar + aBaz->GetStuff(18))) {} If you're blocking execution with && it is understandable to pack if statements with things But for readability and maintainability, things like this are a strain to anyone who comes across it 153 / 248
  104. "Stringly typed" if(userType == "AdminUser") { } Avoid depending on

    information stored in a string Unless you are doing actual string parsing or converting from a string source to a typed result 156 / 248
  105. "Stringly typed" if(userType == "AdminUser") { } Avoid depending on

    information stored in a string Unless you are doing actual string parsing or converting from a string source to a typed result Use Types, Classes, Enums, even integers 157 / 248
  106. "Stringly typed" if(userType == "AdminUser") { } Avoid depending on

    information stored in a string Unless you are doing actual string parsing or converting from a string source to a typed result Use Types, Classes, Enums, even integers Stringly typed is a very common way of turning a compile time error into a runtime crash 158 / 248
  107. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice 160 / 248
  108. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice "Well duh" ... but ... 161 / 248
  109. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice "Well duh" ... but ... I've seen these kinds of mistakes in code written by 10 year veterans 162 / 248
  110. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice "Well duh" ... but ... I've seen these kinds of mistakes in code written by 10 year veterans Not as obvious though, but more nuanced 163 / 248
  111. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice "Well duh" ... but ... I've seen these kinds of mistakes in code written by 10 year veterans Not as obvious though, but more nuanced We found and fixed a crash in Uplay that was "Stringly typed" in disguise 164 / 248
  112. Review of Reviews These mistakes have a common theme Lack

    of Experience and Practice "Well duh" ... but ... I've seen these kinds of mistakes in code written by 10 year veterans Not as obvious though, but more nuanced We found and fixed a crash in Uplay that was "Stringly typed" in disguise I've seen a gigantic function in live ATC code because splitting it up was "Harder to prove correct" 165 / 248
  113. On the job Let's imagine being a C++ programmer We

    are ok with the language but not super secure 168 / 248
  114. On the job Let's imagine being a C++ programmer We

    are ok with the language but not super secure We are tasked with upgrading a part of an older system 169 / 248
  115. On the job Let's imagine being a C++ programmer We

    are ok with the language but not super secure We are tasked with upgrading a part of an older system And in the section we are now, the best solution is a struct with two integers 170 / 248
  116. So far, so good Then someone tells us "Please make

    sure everything is correctly initialized, we can't have junk values here!" 174 / 248
  117. Zero is default? Right? struct VeryImportant { int myOne; int

    myTwo; VeryImportant(): myOne(0), myTwo(0) {} }; 175 / 248
  118. One step at at time Then someone tells us "I

    want to be able to set the values when I create the object" 177 / 248
  119. We got this! struct VeryImportant { int myOne; int myTwo;

    VeryImportant(): myOne(0), myTwo(0) {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} }; 178 / 248
  120. Oh no... Then someone tells us "This object has to

    be correct when it's copied, can you do that?" 180 / 248
  121. The rule of how many? struct VeryImportant { int myOne;

    int myTwo; VeryImportant(): myOne(0), myTwo(0) {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} VeryImportant(const VeryImportant& aVery) : myOne(aVery.myOne), myTwo(aVery.myTwo) {} VeryImportant& operator=(const VeryImportant& aVery) { myOne = aVery.myOne; myTwo = aVery.myTwo; return *this; } }; 181 / 248
  122. Who is this person? Then someone tells us "We upgraded

    to the new compiler, it supports move semantics, can you make sure everything is movable?" 183 / 248
  123. struct VeryImportant { int myOne; int myTwo; VeryImportant(): myOne(0), myTwo(0)

    {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} VeryImportant(const VeryImportant& aVery) : myOne(aVery.myOne), myTwo(aVery.myTwo) {} VeryImportant& operator=(const VeryImportant& aVery) { myOne = aVery.myOne; myTwo = aVery.myTwo; return *this; } VeryImportant(VeryImportant&& aVery) : myOne(aVery.myOne), myTwo(aVery.myTwo) {} VeryImportant& operator=(VeryImportant&& aVery) { myOne = aVery.myOne; myTwo = aVery.myTwo; return *this; } }; 184 / 248
  124. What have you done? Then we get a call "Why

    is our new VeryImportant struct slower than our old library?" 186 / 248
  125. Searching starts You look over your code, nothing You look

    at all the call sites, nothing 189 / 248
  126. Searching starts You look over your code, nothing You look

    at all the call sites, nothing You start doing git blame diffs, nothing 190 / 248
  127. Searching starts You look over your code, nothing You look

    at all the call sites, nothing You start doing git blame diffs, nothing You go into assembly... 191 / 248
  128. "Why is my code slow" You start googling something like

    that Sprinkle in memcpy and structs 195 / 248
  129. "Why is my code slow" You start googling something like

    that Sprinkle in memcpy and structs A certain word keeps popping up 196 / 248
  130. "Why is my code slow" You start googling something like

    that Sprinkle in memcpy and structs A certain word keeps popping up Trivial 197 / 248
  131. "Objects of trivially-copyable types are the only C++ objects that

    may be safely copied with std::memcpy" 198 / 248
  132. struct VeryImportant { int myOne; int myTwo; VeryImportant(): myOne(0), myTwo(0)

    {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} VeryImportant(const VeryImportant& aVery) : myOne(aVery.myOne), myTwo(aVery.myTwo) {} VeryImportant& operator=(const VeryImportant& aVery) { myOne = aVery.myOne; myTwo = aVery.myTwo; return *this; } VeryImportant(VeryImportant&& aVery) : myOne(aVery.myOne), myTwo(aVery.myTwo) {} VeryImportant& operator=(VeryImportant&& aVery) { myOne = aVery.myOne; myTwo = aVery.myTwo; return *this; } }; 200 / 248
  133. So what am I allowed to do? What is 'Trivial'?

    A trivial copy constructor is one not made by the user 203 / 248
  134. So what am I allowed to do? What is 'Trivial'?

    A trivial copy constructor is one not made by the user A trivial move constructor is one not made by the user 204 / 248
  135. So what am I allowed to do? What is 'Trivial'?

    A trivial copy constructor is one not made by the user A trivial move constructor is one not made by the user A trivial copy assignment operator is one not made by the user 205 / 248
  136. So what am I allowed to do? What is 'Trivial'?

    A trivial copy constructor is one not made by the user A trivial move constructor is one not made by the user A trivial copy assignment operator is one not made by the user A trivial move assignment operator is one not made by the user 206 / 248
  137. We're back here struct VeryImportant { int myOne; int myTwo;

    VeryImportant(): myOne(0), myTwo(0) {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} }; 207 / 248
  138. But there is a catch There is a thing called

    std::is_trivial I thought at first that was the one to use 210 / 248
  139. But there is a catch There is a thing called

    std::is_trivial I thought at first that was the one to use std::is_trivial is 211 / 248
  140. But there is a catch There is a thing called

    std::is_trivial I thought at first that was the one to use std::is_trivial is std::is_trivially_copyable 212 / 248
  141. But there is a catch There is a thing called

    std::is_trivial I thought at first that was the one to use std::is_trivial is std::is_trivially_copyable AND std::is_trivially_default_constructible 213 / 248
  142. Also a big no no struct VeryImportant { int myOne

    = 0; int myTwo = 0; }; 219 / 248
  143. is_trivially_default_constructible: 2 And to what benefit? Not sure, some talks

    about what type is allowed in a constexpr context 221 / 248
  144. The only healthy choice #define ENFORCE_TRIVIAL(t) \ static_assert(std::is_trivially_copyable_v<t>); \ static_assert(std::is_trivially_copy_assignable_v<t>);

    \ static_assert(std::is_trivially_copy_constructible_v<t>); \ static_assert(std::is_trivially_move_assignable_v<t>); \ static_assert(std::is_trivially_move_constructible_v<t>); \ static_assert(std::is_trivially_destructible_v<t>); #define TRIVIAL_STRUCT(name, ...) \ struct name __VA_ARGS__; \ ENFORCE_TRIVIAL(name); TRIVIAL_STRUCT(VeryImportant, { int myOne; int myTwo; VeryImportant(): myOne(0), myTwo(0) {} VeryImportant(int aOne, int aTwo): myOne(aOne), myTwo(aTwo) {} }); 222 / 248
  145. Copy elision, RVO, NRVO, oh my What does NRVO stand

    for? Ok, what are we optimizing? 226 / 248
  146. Copy elision, RVO, NRVO, oh my What does NRVO stand

    for? Ok, what are we optimizing? Is copy elision ever guaranteed? 227 / 248
  147. Copy elision, RVO, NRVO, oh my What does NRVO stand

    for? Ok, what are we optimizing? Is copy elision ever guaranteed? What about constexpr? 228 / 248
  148. Understanding the RVO family For me, I understood them fine

    But I never knew when they would be applied 233 / 248
  149. Understanding the RVO family For me, I understood them fine

    But I never knew when they would be applied Again, I had this insecurity, "Was this an RVO or not?" 234 / 248
  150. Understanding the RVO family For me, I understood them fine

    But I never knew when they would be applied Again, I had this insecurity, "Was this an RVO or not?" Until I saw Jon Kalb's lightning talk on Copy Elision 235 / 248
  151. Call To Action I'm not going to spoil his talk

    This is your homework, it might force you to think about the RVO family in a different way 239 / 248
  152. So now what? We all learn differently We all teach

    differently Even chatting with co-workers, we are teaching in some sense 242 / 248
  153. So now what? We all learn differently We all teach

    differently Even chatting with co-workers, we are teaching in some sense So when something seems confusing to someone else 243 / 248
  154. So now what? We all learn differently We all teach

    differently Even chatting with co-workers, we are teaching in some sense So when something seems confusing to someone else Try explaining it in a completely different manner 244 / 248
  155. So now what? We all learn differently We all teach

    differently Even chatting with co-workers, we are teaching in some sense So when something seems confusing to someone else Try explaining it in a completely different manner You might learn something about it yourself 245 / 248
  156. Very Holistic "What really is the point of trying to

    teach anything to anybody?" 246 / 248
  157. Very Holistic "What really is the point of trying to

    teach anything to anybody?" "What I mean is that if you really want to understand something, the best way is to try and explain it to someone else. That forces you to sort it out in your mind. And the more slow and dim-witted your pupil, the more you have to break things down into more and more simple ideas. And that's really the essence of programming. By the time you've sorted out a complicated idea into little steps that even a stupid machine can deal with, you've learned something about it yourself." - Douglas Adams 247 / 248