C++ Code Smells - Jason Turner - CppCon 2019

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
go for this see what's what's code smells this is the talk you're planning to come to I assume my name is Jason Turner if you don't know who I am I'm host of CBB cast goes to CBB gas which is a podcast for C++ developers we're on 214 episodes now and C++ weekly my youtube channel I just published episode 184 I think so 184 weeks straight of doing YouTube videos and I am a Microsoft MVP I do training and contracting and this is my life so this is my sales pitch for myself here if you don't know this you probably want to be closer to the front so you can be more engaged you don't come any closer it's close enough up here yeah please interrupt me and ask questions I feel a little trapped on this tiny stage we'll see if I escape from it depending on how the talk goes but I'll try to not get feedback into the PA like I did a minute ago and this is approximately my training looks like I do still have a context for class coming up on Saturday if you are interested there's still time to sign up for it alright talking about code smells I'm gonna start by talking a little bit about best practices the core guidelines have about 496 items and them who has read all of the core guidelines oh yeah a couple of usual suspects in the room that I recognized but other than that effective modern C++ by Scott Myers has 42 that does not include Scott's effective C++ more effective C++ effective STL I think those are all four of his most popular effective books I have a C++ best practices website if you didn't notice there was a link to it a couple of slides ago that CBG best practices comm it's just a four Keable github coding standards document I've got like 109 things up there see of us most coding standards from herb Sutter Andre Alex and Rescue has a hundred and one items what's that oh the codes new coding standards I agree it is old it's from 2000 however there's still a lot of interesting information in it I still like the book so just from these four items I have 748 approximate best practices this has questions how many of them are unique does anyone know how many unique best practices have been published about C++ I don't know how many of them are like actually like you know how many are important I mean like how many like really affects the way that we write code and we have to keep in mind all the time which ones can tools tell us about all kinds of questions herb at his CV pecan 2018 keynote said we don't have to teach things all compilers warn on part of that problem is getting people to use their compiler warnings too though all right code smells I asked the question to myself is it possible to swap these things around and instead of saying I have 700 rules to apply look for just a couple of things that are smells that help me apply the other 700 rules we're gonna see if that's possible do the smells help us reduce the set of best practices okay I asked Twitter for their favorite C++ code smells this is where some of you will see your own names come up in a moment what do you think of this code this is interactive remember go for it bad Neal some out yeah Ben said it has a split between Becca declaration initialization so I'm just going to click on this and just demonstrate because they've got compiler Explorer and all my slides this is with oh three c++ seventeen mode the latest GCC has been released nine point something and let's just say for the moment it generates code okay this is not going to be an exhaustive assembly language talk until later maybe okay construction and assignment i had construction separate from assignment quotes been dean here this was this was ben's response on twitter in fact so he's right here to give us the live version of it now anyone tell me what's gonna happen if i look at it at an optimized build of this code it's absolutely nothing can someone tell me like why we expect most compilers can optimize this that's well wait who said that okay i'm huh I'm breaking out sorry what'd you say it doesn't affect anything outside the function that's a little bit yes that's true but what might it have affected outside the function global side-effects of like keep a location for exists an example small string optimization is what we've had here a small string buffer is coming into flight which is why it's not doing a heap allocation which is why we see it optimizing it away clang would still optimize it away and regardless okay so that's the first smell or the first yak smells construction separate from assignment now what do we think I'm using output parameters that's what I'm looking for it there sorry I heard the parameters not cost we'll come to that in a moment but I just waited a minute to see if I heard the answer I wanted to hear okay so I can slot this around yes Oliver Oliver Oliver although sorry just got a a t-shirt for you because of us was that already Ollie just got okay so this is output variables this was his codes no quick question how many parameters is this function take what uh-huh that's not a very clear answer yes go ahead you have to yell your hand was up just yell no no are you in front in front yes you and undefined what how's it an undefined number of arguments what am I missing yeah the declaration is right there oh okay can anyone give me a specific answer one parameter what does that one parameter as the return value location where the memory location for that function to go I just I lied when I said we're not going to talk about assembly I'm sorry but it wasn't intentional I promise right here right here I am setting up a place on the stack right above that for the string value that's going to result from this function I'm saving that in the the register R T I and the 64 bit calling admission CRDi is the first parameter to the function it has an unknown a hidden first parameter that's the location the string is going to live in okay output variables or out variables there's also a recent ish article last few years from Sean parent on stop using out arguments here you can go and read that if you would like to think you know the rules already to start yelling stuff out yeah what's that you would like to use an algorithm on the vector yeah I would agree with that does anyone else agree with that no raw loops raw loop so a raw loop is the Cold's code smell we're looking at here Shawn parent he didn't actually say this on Twitter to me but raw loops don't express our intent algorithms can now I know that this code right here on line 12 some of you might think it's insidious and some of you might love it who says this is terrible hooey I'm getting people I know saying it's terrible who loves it okay all right that's cool it was more yes Ben okay Ben wants me to use C begin and C and that I will not argue with what's the in-between comment yeah no I mean but where would you do you want to make a comment as to what you don't love or don't hate okay to me that the power in this kind of expression is being able to read it are all of them in the range everything from the beginning to the end and range right like that's that's what I love about it does anyone who hates it when a comment yeah go ahead for it you're on okay so you don't love the fact that we don't have C both lost 20 ranges yet is that what you're saying you don't like the begin and end is that's what you're saying you don't like so if I could just pass in the values to the Olive algorithm and then it would be awesome okay so you both lost 20 ranges we'll get there soon ish what year is this 2018 wage who is using C++ Jeanne currently at work how okay let's do this now slightly differently at what point were you allowed to start using C++ 17 so I I don't know how do I do this as a group hall did you start using C++ 17 this year did you start using it last year the year before that that would be a little bit harder but possible okay so it sounds like a lot of you and here are actually pretty able to stay close to the top of C++ standards that's cool so maybe perhaps in 2021 you will be able to use ranges at work if your company keeps up on the standards so raw loops Shawn parent yeah I get get put those in a function you can give them a name I am NOT going to argue with that at all [Music] someone just said bracket access I wasn't planning to talk about that at all who said that okay why did you say it unintentional rights as a result of bracket access you know what I would say that's possibly true because in this hypothetical code those pipes and hoses are member variables and I didn't make the total area member function cost I really should have I didn't even think about it when I wrote this example yeah that's interesting point oh did you not like that part okay so I'm sorry for the camera that's hose radius times type radio' pipe radius that one right here yeah which can be solved he'll then using them func okay let's go ahead yeah so that's a bug let's decompose this into functions and/or lambdas I I don't care lambdas are awesome right who says lambdas are awesome are they our favorite feature of C++ no all right so lambdas are awesome but not necessarily our favorite feature of C++ so I can you know make this into you know total area plus total area something like this Multi step functions this is this is actually what I was getting at here is this step one step two step three step four it was a Toni van eared tweeted about this so much of people responded to him on this and other people are like prepping their code do I have anything that says step one anywhere in my code because if you do it probably needs to be multiple functions wait what's that tell you is we're not there yet all right on that one yes value is int does that bother you what what is the type of pipes what is the type of hoses you don't know right but I multiply it by PI maybe it's a very inaccurate no yes okay yes value is an int we'll come back around to that in just a moment okay our comments necessary in this code does anyone want to take a dissenting opinion and make your stand what's that you might have new employees now I've actually well someone who's in this room at the moment took one of my classes and said if you think that a comment is necessary that means you didn't name things well enough and I don't know if I agree with that 100% but I'm very very close to agreeing with that so you could have potentially named things better if your new employees might be confused by this code what's that I am NOT going to say out loud on the record that comments our code smell but they might be that okay so the comment I know this quadrant heard this was the default reference capture is a definitely a code smell when does it matter as asynchronous functions okay it matters when the lambda escapes past the lifetime of the objects that you captured I am calling this lambda right here in this function it's a good point it's a strong point yeah no that's a good point in this example so I actually had this example written differently earlier but I separated it out if I put this lambda called accumulate area inside this lambda called total area the only reason I'm capturing it all right here's to capture the other lambda I could have captured it by value actually it would have had the exact same effect because that is a stateless lambda and it would have been fine or I could have defined the lambda inside the other lambda and it would have been equally fine or I could have moved any of these out into named functions to solve that problem but it's a good point I could use transform reduce know your algorithms okay I'm gonna move on from that because I'm spending perhaps maybe more time than I meant to on it multi step functions Bjorn not here Tony is not here as Peter and the room by any chance oh they don't get to shout out any shout outs here okay who you wanted a free comparison operator I'll give you that one but it's not I'm going for I'm missing at least one Const how many constants - okay oh just for the record by the way like all of these examples are based on code that I've looked at in recent memory so it's not just completely arbitrary examples spaceship yeah and in two years you get to use spaceship okay Const Const member function constant values a comparison operator this is cost we're not mutating our objects of cost with comparison operators non canonical operators I realized like five minutes ago that I should probably have numbered these sorry okay going back to this one what was the the shout back there our value is integer that's one problem what other problems do I have now I fixed it that on the slide yeah sorry yeah what's that MPI is double yes it won't annoy so we know that yeah so value is an integer with those should all be doubles but there's another problem besides the values in the raid I and the PI my index type is an int just out of curiosity does anyone actually work on datasets bigger than two billion items yes Bryce does no question there's a few in the room yeah it's real yes it's real right I work a lot with a company that does database systems they're like two billion that's nothing right so those should be size type const o's string to const car pointer back to string again as the conversion i'm doing here yes i'm forcing a copy of the data but even worse than that i also have to call sterling to see how long the data is cuz that information got lost who could actually step into this and see the call to sterling if you wanted to but let's not do that at the moment i am making a strong statement here i say this always exists in code that has been reflected over a long period what's that you want a warning to catch that we'll come back to that anything else i know that i am making the camera crews job more exciting yeah I don't know I don't know if I'm gonna what's that should I use string view maybe I definitely don't want to be doing this conversion back and forth I would say they call to see string is a bigger smell but either way whoo yeah I can do that call call get string in there yeah yeah yes okay we can pick up I it's amazing I did a live code review recently as a part of a user group Meetup and weed couldn't get past the first four lines of the hey it was it was the thing ah anyhow who can tell me what standard knew this it's a cast it's an unconditional cast someone said - what - an r-value reference okay and conditional reference it cast an order value reference you're being intentionally difficult in the front row okay I'm Ganesh no gas no string r-value reference I just eliminated our yeah what are you gonna say sorry yes sort with it is a long string intentionally well at least on some versions of standard string yeah so I'm basically I'm forcing a move instead of letting the compiler do its rvo is that also what you are indicating you're saying the move is expensive because sort string operate I don't think this is the smooth anymore expensive or less expensive it's a small string I'm gonna ask the standard library implementer in the front row what's that it has no relevance here whether or not it's okay good the definitive answer alright so this is our professed Pesa maizing return by move prevents move illusion move prevents our view so I'm talking about code of conversions at the moment I kind of glossed over that topic standard move this is another type of conversion that is a code smell huh why I am making a general statement that move is a code smell yes I am yes there was a talk not given by me that was called nothing is better than moving it was last year it's to be pecan and I believe the point is doing nothing is better than moving [Laughter] yes okay guaranteed copy illusion is not applied this breaks guaranteed guaranteed our Bo isn't 17 it's only guaranteed in the case that we initialize that value in the return statement if so your co-worker says it's not guaranteed so don't return by value in your coworker once to return by move instead they're always wrong no they're not always wrong actually have some examples on Friday where they're not always wrong but these kind of examples are trivial to just pop into compiled explore and show who look it's doing more work than it has to we'll get to that I won't repeat what you just said okay real quick though what's returned from this function from main here ooh I think I need a better so Doom is returned does anyone know like what your compiler is actually gonna do here anything they all that I am aware of will do this so you're signing 13 and it just returns 4 because it's allowed to take that constant and assume that it's con store the rest of eternity that that value doesn't change so 4 is in practice returned but in reality this is undefined behavior and the big compiler catches on fire and it's bad so modifying Const abdic turning its lifetime is undefined behavior Const cast is another explicit conversion that is a code smell weak typing sarna tweeted this out and Matt yeah the witch inches which interface right when we're calling socket do we know which one of this is is that the domain the type the protocol right this is not pleasant kinds of API store quit so code with conversions this is the code smell that wraps up the last thing implicit explicit cast I'm including move in that for the moment well I'm including it but believe me for the moment Kenner compilers warned on this yes if we turn on our warnings right so we can get warnings here on loss of precision hopefully just for the record ooh let's who uses Visual Studio oh my goodness that is a high percentage who uses GCC order uses GCC exclusively okay you have a lot of warnings to turn on because GCC is the worst about warning on a lot of these and loss of precision kind of things Visual Studio in my opinion is the best data can we get a warning here what's that yes I'm getting some thumbs up in the background what do you say yeah claim can worn on it which warning specifically are you bringing - yeah okay so who I don't have it spelled out here we can definitely get warnings from our tool chains on here clean tidy can definitely worn on this Pesa maizing move right yeah one okay so core dakotas mornings this was yarn and Dimitar Miuccia perhaps commented on coded warnings here what are the implications of putting a static variable right here the static string function name there's a guard every time we go to access this it's a magic static c++ you love in guarantees that these things have to be initialized in the thread safe way since all of you raised your hands that you are both visual studio programmers and you're using c++ 17 that means you all also have this feature if we actually really wanted to walk through this we can see it here we can see that there's a guard variable we have to do a guard acquire every single time we enter this function or well not a guard acquire we have to check to see if the value has been initialized and if it has not then we have to come down and acquire a lock guard so that we can initialize that then we have to release the lock guard otherwise we have to jump over that code and exit from the function that's it's not free each time yes that's what I just said compared to this now I cheated a little bit I got a question about string view earlier but if I'm just logging errors string view sounds pretty good that sounds like something that's observing a string like thing to me so I can use a context for a static string view he now what work do I have to do whenever I enter this function I I do I never have to reinitialize the string no it's static yeah so this is a static string view initialized with a character literal that character literal is guaranteed for the lifetime of the program this is going to be initialized at compile time we're just we're not gonna see really anything that has to happen here I'm just making sure I'm getting not a couple people because I'm like wait a minute did I doesn't miss something no what was that okay so logically speaking the question was any any advantage to dropping or keeping the static logically speaking this is static right we only want one value of this thing so I think let's just say it's static practically speaking if we don't make it static many compilers actually well this is a string view so it's not a complicated thing but if it were something that actually performed a calculation many compilers would actually reinitialize this value even though or a constant four on each function call for making concepts or static they won't so it's it's more logically correct and it is going to gain you something so this is my next code smell here static Const is a code smell it should probably be context / not everything can be contact spur you should come to my class on Saturday no not everything can be context / but most things things that are known at compile time static Const is likely something that can be calculated at compile time there's a very very good chance that can be made context / it might take more work than you want to but it's a very high probability initialize it somewhere else potentially yeah so we don't have to do this yeah so I saw this code recently greatly simplified into three little code walks I'm going to simplify it one step further and just look at this this is what the compiler sees when it cut when it compiles the CPP file it can't be in the value while the value can't be inlined as more to the point here so this is sorry camera person I'm walking around it's kind of like I'm telling the compiler I have some really important information for you but I'm not gonna tell you right here whenever I go to access this value I have to go look at some memory location and say what is this value it's a Const static the gods integer right that is known at compile time in this code it definitely wasn't this code just for the record some code it's not it's known at lean time I'm telling you in this code it was known at compile time okay better option static constant the header or context for compile time constants context per X turn Const often can also be constant for all right yeah man you'll marry much I'm calling new we don't like that Rondo and delete I can use make unique this is of course wasteful and the heap should be avoided if at all possible just needed a throw new and delete in there so it's so far my code smells construction as separate from assignment output variables raw loops multi-step functions non-canonical operators code with conversions including move casting away const code with warnings actually I just was n Billy of O'Neill's talk and he is talking about casting away constant some wow really bad ways static Kant's extern con so he was talking about bad code that other people had written not code that he was advocating just for the record raw new and delete now this does not cover everything right I said we're starting at 478 things I see lots of you taking pictures were not to the good stuff yet okay let's do a quick code review what do I do what was that sorry you're gonna have to be like yell using namespace okay let's get rid of that initialize link no oh I should point out just for the record that this code is meant to be learners code so we still want to leave the addition of the two strings together right like this is just code about learning how to use string objects okay what now Const everything I just heard and I don't know just to satisfy like a third of you I'll do this yeah yeah what was that price they include yes it's got the wrong include here let's do that GCC's mmm their headers aren't terribly clean including iostream picks up stirring it really I mean was there a warning from it oh there is a warning okay that also could be Const okay what should the type be oh yeah we're gonna go for the technical answer yeah okay who prefers stood string size type who prefers Auto who prefers s ice-t all right Auto wins okay what's that maybe I'm using uh yeah I mean yes you're right we could yeah that's that's silencing the warning you stood sighs oh I you know what I'm not that you know you're just gonna make me run out of time and I'm gonna blame Ben and it'll be on the record that black Ben is to blame I am not in the habit of using the pre-function for size personally I am for begin and end and many of the other things Oh Bryce doesn't want me to do this Ben justify yourself what's the advantage to need a mic no he's not gonna justify himself okay I honestly don't have an opinion stood sighs what wait yes it so if you're writing generic code stood sighs would work in this case yeah okay I'm gonna move on here we've ended up with something kind of like that I think except the iostream header oh and then using namespace alright stood in line oh oh you're saying here yes I'm gonna agree with that that's a surprise that I agree with that right okay Oh see behind the curtain okay all right what else transform I mean I'm counting calculating calculating a factorial I don't know if I can okay so I could use accumulate or transform or something to generate this out to accept any of those answers also though I don't think I'm gonna take the time at the moment to type them in what's that declare the very act to clear the variable inside right okay you're saying like that okay [Music] okay the actual comment was I don't need to return from Maine so I just decided to be explicit about the meaning of the return value okay I mean is anyone gonna suggest like maybe a factorial function as a hypothetical possibility here your needs big end so he said yeah I mean factorial could quickly blow up there's the hand way back there huh were you just scratching your head alright okay not not to pick this apart line by line but you know maybe I ended up with something kind of like this so I've got a read input function I've got a factorial function and now I can initialize my variables when they're declared I can read the input in this case I made a generic I could have done whatever and I have a factorial function so I can ignore initialize the factorial variable and then I can print this out and I can return a constant with meaning is that okay the codes longer yeah go for it yes oh if you wanted to read multiple variables so you're saying if you did like C n extraction operator value one value to value three value for I don't honestly think that that would be more or less efficient than this but I've never measured it I don't think CN is the paragon of reading efficiency in the first place yes Renee is it I'm not yes and there yes and no way is this code sanitizing input or checking that's true yeah but is that I see boss plus code smell or is that just generic code smell yeah programming smell yeah I'm just coming back there yeah I'm just keeping it exciting instead of a role Luke creating a range 1 to 10 and then accumulating or 1 to n I think the problem with that would be I'd have to store that range somewhere and that would likely involve a heap allocation or something that is unnecessary for the sake of this code unless I am missing something unless I did a generator what's that use iota ben says use iota yes we could do that ok I didn't need to reload that page we had not done anything what are the conclusions that we've come to so far it's one thing that keeps coming up that hasn't really been explicitly mentioned when I walk through these review what do you see here Const what do you see here fewer lines of code yes Const what better syntax highlighting there's nothing to syntax I delight on the first block so you've more keywords so that your code is more colorful is better code smells good ok Const what do we see here using the algorithms more readable and more constable is that a word is that a bull or a ball were constable so what kept coming up Const it's not like this is the first time that cons has ever been mentioned at a C++ conference now this I don't know if this is going to work because is this Mike hot now can you make the podium Mike hot sound engineer okay it says these it says it's hot now I don't know China I get feedback or anything is that no that's not happening my my cup Chua okay I'm just gonna stop here that's all right so this is Kate Gregory at CB peak on like 2014 her first the first CVP con she did a AJ and I talked with James McNellis and she's saying don't Const because you have to Const everything that you possibly can and the compiler becomes your friend so you're going through this is constant this is modernizing modern Inge legacy C++ code they go through and she advocates making every single variable cost that he is possible to make cost and then the compiler checks you when you're modifying things that you shouldn't be modifying yes yes she is yes she did say last year also make everything Const and then keep removing constant till it compiles that's starting with legacy legacy code base I also have a little snippet here of myself from my first major CV pecan talk we're adding Const makes like the entire program go away because adding Const on a static global variable let the compiler put it into the read-only space of the binary and then it applied a whole other set of optimizations to the code not the kind of thing that we can rely on Const Const isn't about optimization but it might get you optimizations accidentally so cost any lack of Const is a code smell I'm gonna say that strongly yes okay const forces us into more organized code we're calling our algorithms we're using functions kant's prevents common errors constant Courage's more easy algorithms are I'm repeating myself now do you constable you parameters you're communicating with the other people I get YES on the definition only this just for the record is code that I actually wrote except I made count Const and it didn't compile I got to the plus plus count and I'm like why am I getting a bill there I have a couple of people agreeing with me here I know full well if you go back to your companies and you say Jason said make all of my value parameters Const they will all shoot you down and tell you that I'm crazy and stuff like that but I'm standing on this one I am if you use STD move than you need parameters that are not Const yeah maybe but then you're doing something different so if you need to call move on it I didn't say never use cost I just I mean I said I didn't say never use move I said it as a strong code smell if you need to move an object then it can't be Const I know it does it's wrong no sorry ah the comment was clean tidy yeah yells at you for doing that I know I know yeah I like I said not everyone abuses me although I am I am pleased with some front row agreement at the moment okay do you Kant's temporary values this is my temporary value do i conce this all right what about line eight what is that that's wrong so I will do a temporary well there's no guarantee that any move is actually which is it Americans no it'll it'll construct my copy in place of the parameter no extra move will happen it'll just be a copy here okay so I'm applying Const okay so that reverts to a copy now I made it not Const I'm sad because I wanted to move it how do I solve the problem what's that like just put it in the invocation solve the problem good for us we avoid the temporary we can write a function okay DUI Const return to objects this gets tricky and I'm running out of time so as this okay who says this code is perfectly fine and it does what we want it to do oh okay who says who says they they think it should be fine but they're not sure okay who says this is definitely terrible okay quick someone definitely terrible why so it does not inhibit return value optimisation right here now I'm going to do this now I get no rvo and I get to construct two string objects whether or not I need them if I do this this by the way is implicitly a move it's equivalent to doing this this is a copy because it's Const this is a copy because it's caused so how do I resolve this problem don't use Const wrong answer okay I can still use Const I can do this I get our vo and both of the code paths here or I can avoid naming the temporary altogether this by the way I was building clang for the fun of it before this conference well before the last conference has that just to like get a fresh build of a recent trunk build of clang and I saw this code come up repeatedly generating warnings claying compiling clang was generating warnings on code doesn't even know what it is that it was generating yeah yeah redundant move is what it was actually called because it's a move it says I see the plus eleven okay do iconst value return types no why not because what actually okay I got a few comments that say it does nothing but let's we now we have to clarify that okay this by the way the question is do I want to forbid this kind of operation it's weird right I don't want to do that but if I put Const here that now fails to compile it's not allowed of built-in types so if I do this if I have a local string object s and I assign it this is move assignment from the return value from this function if I do this it is now copy assignment because you can't move from a constant value it does actually change the meaning of the code of course we can avoid this question altogether we can use cost so it becomes Arella but it becomes irrelevant right I just said wait a minute that string s that's not cost how do I make it Const oh I can make it constant now if it's constant is even matter anymore it's not an assignment anymore so we don't care so I found three smells missing and ignored compiler warnings there are special checks for many of these CBP check helps us reduce variable scope variable can be Const can also come from various tools I'm missing a quote unfortunately the court guidelines checkers will check us for raw pointer memory usage Pesa maizing move warnings Const return values and cling tidy wait who was it who said you want a compiler warning the string conversion it was you yes so claim tidy actually has a special check specifically for that case when you're taking a string and passing it as a C string to another string so claim tie to use your tools yeah this is the good stuff by the way now you can take a picture missing Const and context / or misplaced Const why is that value or member function not Const if it's known at compile time it should be a context / or enum make it a compile time Ponte a constant this forces us into more efficient organizing of code if we know the size of a thing and we want to make it Const or we want to make a constant expert now we're using stood array which is amazing we're using algorithms we're using the numeric header and you have my little quote just for the record I am quoting myself down here I don't care if you want to use east or west Const use Const and also I'm not an almost always Auto fan but using almost always Auto pushes us in the same direction of initializing our values music cost week types and casts unfortunately the C++ standard library doesn't help us here a whole lot we have implicit conversions helpfully between string file system path cost character pointer string views we've got implicit conversions into optionals variants and between shared pointer types and unique pointer types lots of non explicit constructors and conversion operations and the standard library so you can do what you can on your side use stronger typing rules one and two catch some of these things otherwise you just have to read your code sorry no I don't have any slides for that yan yan sorry well okay maybe I will just go ahead mention I don't have a slide for it but I probably should have member variables should not be caused that breaks other things so let's just throw that out there right now I should have had a slide on in that I apologize member variables not Const remove that from your from your brain and we can avoid naming temporaries to avoid move and I don't think I have time for the bonus code review a whole lot here but go ahead quickfire I have like two minutes const reference vector what next element Oh minimax element yes minimax element and that's it so I can just make my own algorithm right my algorithm is to return the range of values from here the min and the max or the sorry the range the distance from them into the max I actually looked it up on a math website to make sure I had the term right for range that's how I roll okay so uh that's me we have like two minutes for questions if anyone has any questions yell it out or go up to a microphone or just come up to the stage and join me up here if you want to why isn't constant default yeah historical reason yes it is on lambdas lambdas get the defaults right and then Moodle becomes the smell yes but we can't do it now why shouldn't member variables because because there's weird lifetime rules that come into play that like if you change the location of a thing and it's bad and then that gets ya anyhow and then there's a they just makes your types much harder to work with than they need to be you can't swap them it's harder to use them in containers you can't assign to them and that kind of thing yeah all right thank you everyone you
Info
Channel: CppCon
Views: 55,544
Rating: undefined out of 5
Keywords: Jason Turner, CppCon 2019, Computer Science (Field), C++
Id: f_tLQl0wLUM
Channel Id: undefined
Length: 58min 34sec (3514 seconds)
Published: Wed Oct 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.