Share
Embed
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
Transcript
248 slides
-
Who is "The Icelander" Ólafur Waage Malmö for the last
4 years 4 / 248 -
Who is "The Icelander" Ólafur Waage Malmö for the last
4 years Senior Programmer at Ubiso Massive 5 / 248 -
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 -
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 -
This talk The structure of this talk is 4 stories
about learning 8 / 248 -
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 -
Let's talk about learning First, some preamble Nobody learns in
the same manner 11 / 248 -
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 -
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 -
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 -
Visual Learner Abstract Images Scribbling figures into a notebook Drawing
diagrams and how they relate to other diagrams 20 / 248 -
Auditory Learner Learn by listening Asking questions for clarification Group
discussions Use of mnemonic devices (Please excuse my dear aunt sally) 21 / 248 -
Read / Write Writing notes, lots of notes Books, Reports,
Essays, all great sources of knowledge Translate abstract ideas into text 22 / 248 -
Kinesthetic Learn by doing Repetition Lots of experiments Change, break
things, figure out why 23 / 248 -
Back to school First semester of University, about halfway through
Programming 101 26 / 248 -
Back to school First semester of University, about halfway through
Programming 101 We're doing C++ (yay) 27 / 248 -
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 -
Pointers We had a two hour class That was nothing
but these kinds of pictures with code on the side 32 / 248 -
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 -
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 -
Pointers++ Speaker was fine Going through the talk now, there's
nothing wrong in the talk itself (informationally) 37 / 248 -
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 -
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 -
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 -
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 -
*Pointers Any pointer exercise made me scared There was always
this doubt it my mind, that I was missing something 43 / 248 -
*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 -
*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 -
*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 -
*Pointers++ Page 93 ??? Chapter 5 ??? How long is
this book? 53 / 248 -
*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 -
*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 -
*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 -
*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 -
*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 -
&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 -
&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 -
&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 -
*&Pointers Mental models are very important Especially when you are
starting out learning a topic 66 / 248 -
*&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 -
*&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 -
Quick Example Who here knows what a Monad is? Monad
is a monoid in the category of endofunctors, duh! 71 / 248 -
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 -
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 -
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 -
A foot in the door You have a huge library
of stand alone functions that look something like this 75 / 248 -
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 -
Oh no the foot is stuck But it's such a
hassle to call these functions 77 / 248 -
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 -
Please help, it's getting numb What if you want to
chain many of these modifying functions 79 / 248 -
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 -
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 -
Ahh thanks What if there was a language feature where
you could chain together modifying functions like that 82 / 248 -
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 -
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 -
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 -
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 -
Back to the future In November of 2017 I decided
to open up my Twitter DM's 88 / 248 -
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 -
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 -
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 -
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 -
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 -
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 -
Code Reviews - The Bad Having an open DM can
be a negative thing 101 / 248 -
Code Reviews - The Bad Having an open DM can
be a negative thing I got sent a lot of graphic photos 102 / 248 -
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 -
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 -
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 -
Code Reviews - The Good People are so nice and
respectful 106 / 248 -
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 -
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 -
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 -
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 -
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 -
One Function to Rule Them All void IDoEverything_IEvenModifyGlobalState() { //
Pages of code } 113 / 248 -
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 -
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 -
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 -
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 -
One Class to Rule Them All class IContainAllTheCode { //
Visual Studio sometimes crashes when I'm opened } 118 / 248 -
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 -
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 -
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 -
The Pyramid flattened int MoreHopeThisTime( const EnumOfSomeState aIShouldNotChange, const std::vector<Thing>&
aIAmNotCopied) { if(aIShouldNotChange == EnumOfSomeState_Invalid || aIAmNotCopied.empty()) { return 0; } // etc } 122 / 248 -
The Pyramid I feel this comes with insecurity with &&
and || operators 123 / 248 -
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 -
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 -
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 -
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 -
Is magic real? int Magic( int aSomeValue, int aSomeOtherValue) {
if(aSomeValue > 5) // magic number { // stuff } // some other stuff } 128 / 248 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
// 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 -
// 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 -
// 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 -
// 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 -
More comments A big problem with comments is that there
is nothing connecting comments to the code 146 / 248 -
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 -
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 -
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 -
Crowded question Similar problem to one function to rule them
all, a single if statement packed with checks 150 / 248 -
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 -
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 -
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 -
"Stringly typed" if(userType == "AdminUser") { } Avoid depending on
information stored in a string 155 / 248 -
"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 -
"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 -
"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 -
Review of Reviews These mistakes have a common theme Lack
of Experience and Practice 160 / 248 -
Review of Reviews These mistakes have a common theme Lack
of Experience and Practice "Well duh" ... but ... 161 / 248 -
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 -
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 -
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 -
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 -
On the job Let's imagine being a C++ programmer We
are ok with the language but not super secure 168 / 248 -
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 -
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 -
So far, so good Then someone tells us "Please make
sure everything is correctly initialized, we can't have junk values here!" 174 / 248 -
Zero is default? Right? struct VeryImportant { int myOne; int
myTwo; VeryImportant(): myOne(0), myTwo(0) {} }; 175 / 248 -
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 -
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 -
Oh no... Then someone tells us "This object has to
be correct when it's copied, can you do that?" 180 / 248 -
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 -
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 -
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 -
What have you done? Then we get a call "Why
is our new VeryImportant struct slower than our old library?" 186 / 248 -
Searching starts You look over your code, nothing You look
at all the call sites, nothing 189 / 248 -
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 -
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 -
"Why is my code slow" You start googling something like
that 194 / 248 -
"Why is my code slow" You start googling something like
that Sprinkle in memcpy and structs 195 / 248 -
"Why is my code slow" You start googling something like
that Sprinkle in memcpy and structs A certain word keeps popping up 196 / 248 -
"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 -
"Objects of trivially-copyable types are the only C++ objects that
may be safely copied with std::memcpy" 198 / 248 -
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 -
So what am I allowed to do? What is 'Trivial'?
A trivial copy constructor is one not made by the user 203 / 248 -
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 -
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 -
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 -
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 -
But there is a catch There is a thing called
std::is_trivial 209 / 248 -
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 -
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 -
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 -
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 -
is_trivially_default_constructible Quite the beast You are not allowed to default
initialize anything 216 / 248 -
is_trivially_default_constructible Quite the beast You are not allowed to default
initialize anything Anything 217 / 248 -
Not Allowed struct VeryImportant { int myOne; int myTwo; VeryImportant():
myOne(0), myTwo(0) {} }; 218 / 248 -
Also a big no no struct VeryImportant { int myOne
= 0; int myTwo = 0; }; 219 / 248 -
is_trivially_default_constructible: 2 And to what benefit? Not sure, some talks
about what type is allowed in a constexpr context 221 / 248 -
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 -
Copy elision, RVO, NRVO, oh my What does NRVO stand
for? 225 / 248 -
Copy elision, RVO, NRVO, oh my What does NRVO stand
for? Ok, what are we optimizing? 226 / 248 -
Copy elision, RVO, NRVO, oh my What does NRVO stand
for? Ok, what are we optimizing? Is copy elision ever guaranteed? 227 / 248 -
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 -
Understanding the RVO family For me, I understood them fine
But I never knew when they would be applied 233 / 248 -
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 -
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 -
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 -
So now what? We all learn differently We all teach
differently 241 / 248 -
So now what? We all learn differently We all teach
differently Even chatting with co-workers, we are teaching in some sense 242 / 248 -
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 -
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 -
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 -
Very Holistic "What really is the point of trying to
teach anything to anybody?" 246 / 248 -
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